324 lines
6.0 KiB
Go
324 lines
6.0 KiB
Go
package main
|
|
|
|
import "errors"
|
|
|
|
type Base struct {
|
|
name string
|
|
}
|
|
|
|
type E struct {
|
|
// Base
|
|
i int
|
|
}
|
|
type StructWithAllTypeFields struct {
|
|
i8 int8
|
|
i16 int16
|
|
i32 int32
|
|
i64 int64
|
|
i int
|
|
u8 uint8
|
|
u16 uint16
|
|
u32 uint32
|
|
u64 uint64
|
|
u uint
|
|
f32 float32
|
|
f64 float64
|
|
b bool
|
|
c64 complex64
|
|
c128 complex128
|
|
slice []int
|
|
arr [3]int
|
|
arr2 [3]E
|
|
s string
|
|
e E
|
|
pf *StructWithAllTypeFields // resursive
|
|
pi *int
|
|
intr Interface
|
|
m map[string]uint64
|
|
c chan int
|
|
err error
|
|
fn func(string) (int, error)
|
|
pad1 int
|
|
pad2 int
|
|
}
|
|
|
|
type Interface interface {
|
|
Foo(a []int, b string) int
|
|
}
|
|
|
|
type Struct struct{}
|
|
|
|
func (s *Struct) Foo(a []int, b string) int {
|
|
return 1
|
|
}
|
|
|
|
func FuncWithAllTypeStructParam(s StructWithAllTypeFields) {
|
|
println(&s)
|
|
// Expected:
|
|
// all variables: s
|
|
// s.i8: '\x01'
|
|
// s.i16: 2
|
|
// s.i32: 3
|
|
// s.i64: 4
|
|
// s.i: 5
|
|
// s.u8: '\x06'
|
|
// s.u16: 7
|
|
// s.u32: 8
|
|
// s.u64: 9
|
|
// s.u: 10
|
|
// s.f32: 11
|
|
// s.f64: 12
|
|
// s.b: true
|
|
// s.c64: complex64{real = 13, imag = 14}
|
|
// s.c128: complex128{real = 15, imag = 16}
|
|
// s.slice: []int{21, 22, 23}
|
|
// s.arr: [3]int{24, 25, 26}
|
|
// s.arr2: [3]github.com/goplus/llgo/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}}
|
|
// s.s: "hello"
|
|
// s.e: github.com/goplus/llgo/cl/_testdata/debug.E{i = 30}
|
|
// s.pad1: 100
|
|
// s.pad2: 200
|
|
s.i8 = 8
|
|
// Expected(skio):
|
|
// s.i8: '\x08'
|
|
println(len(s.s), s.i8)
|
|
}
|
|
|
|
// Params is a function with all types of parameters.
|
|
func FuncWithAllTypeParams(
|
|
i8 int8,
|
|
i16 int16,
|
|
i32 int32,
|
|
i64 int64,
|
|
i int,
|
|
u8 uint8,
|
|
u16 uint16,
|
|
u32 uint32,
|
|
u64 uint64,
|
|
u uint,
|
|
f32 float32,
|
|
f64 float64,
|
|
b bool,
|
|
c64 complex64,
|
|
c128 complex128,
|
|
slice []int,
|
|
arr [3]int,
|
|
arr2 [3]E,
|
|
s string,
|
|
e E,
|
|
f StructWithAllTypeFields,
|
|
pf *StructWithAllTypeFields,
|
|
pi *int,
|
|
intr Interface,
|
|
m map[string]uint64,
|
|
c chan int,
|
|
err error,
|
|
fn func(string) (int, error),
|
|
) (int, error) {
|
|
println(
|
|
i8, i16, i32, i64, i, u8, u16, u32, u64, u,
|
|
f32, f64, b,
|
|
c64, c128,
|
|
slice, arr[0:],
|
|
s,
|
|
&e,
|
|
&f, pf, pi, intr, m,
|
|
c,
|
|
err,
|
|
fn,
|
|
)
|
|
// Expected:
|
|
// all variables: i8 i16 i32 i64 i u8 u16 u32 u64 u f32 f64 b c64 c128 slice arr arr2 s e f pf pi intr m c err fn
|
|
// i8: '\x01'
|
|
// i16: 2
|
|
// i32: 3
|
|
// i64: 4
|
|
// i: 5
|
|
// u8: '\x06'
|
|
// u16: 7
|
|
// u32: 8
|
|
// u64: 9
|
|
// u: 10
|
|
// f32: 11
|
|
// f64: 12
|
|
// b: true
|
|
// c64: complex64{real = 13, imag = 14}
|
|
// c128: complex128{real = 15, imag = 16}
|
|
// slice: []int{21, 22, 23}
|
|
// arr: [3]int{24, 25, 26}
|
|
// arr2: [3]github.com/goplus/llgo/cl/_testdata/debug.E{{i = 27}, {i = 28}, {i = 29}}
|
|
// s: "hello"
|
|
// e: github.com/goplus/llgo/cl/_testdata/debug.E{i = 30}
|
|
i8 = 9
|
|
// Expected(skip):
|
|
// i8: '\x09'
|
|
println(i8)
|
|
return 1, errors.New("some error")
|
|
}
|
|
|
|
type TinyStruct struct {
|
|
I int
|
|
}
|
|
|
|
type SmallStruct struct {
|
|
I int
|
|
J int
|
|
}
|
|
|
|
type MidStruct struct {
|
|
I int
|
|
J int
|
|
K int
|
|
}
|
|
|
|
type BigStruct struct {
|
|
I int
|
|
J int
|
|
K int
|
|
L int
|
|
M int
|
|
N int
|
|
O int
|
|
P int
|
|
Q int
|
|
R int
|
|
}
|
|
|
|
func FuncStructParams(t TinyStruct, s SmallStruct, m MidStruct, b BigStruct) {
|
|
println(&t, &s, &m, &b)
|
|
// Expected:
|
|
// all variables: t s m b
|
|
// t.I: 1
|
|
// s.I: 2
|
|
// s.J: 3
|
|
// m.I: 4
|
|
// m.J: 5
|
|
// m.K: 6
|
|
// b.I: 7
|
|
// b.J: 8
|
|
// b.K: 9
|
|
// b.L: 10
|
|
// b.M: 11
|
|
// b.N: 12
|
|
// b.O: 13
|
|
// b.P: 14
|
|
// b.Q: 15
|
|
// b.R: 16
|
|
t.I = 10
|
|
// Expected(skip):
|
|
// all variables: t s m b
|
|
// t.I: 10
|
|
println("done")
|
|
}
|
|
|
|
func FuncStructPtrParams(t *TinyStruct, s *SmallStruct, m *MidStruct, b *BigStruct) {
|
|
println(t, s, m, b)
|
|
// Expected:
|
|
// all variables: t s m b
|
|
// t.I: 1
|
|
// s.I: 2
|
|
// s.J: 3
|
|
// m.I: 4
|
|
// m.J: 5
|
|
// m.K: 6
|
|
// b.I: 7
|
|
// b.J: 8
|
|
// b.K: 9
|
|
// b.L: 10
|
|
// b.M: 11
|
|
// b.N: 12
|
|
// b.O: 13
|
|
// b.P: 14
|
|
// b.Q: 15
|
|
// b.R: 16
|
|
t.I = 10
|
|
// Expected:
|
|
// all variables: t s m b
|
|
// t.I: 10
|
|
println("done")
|
|
}
|
|
|
|
func main() {
|
|
FuncStructParams(TinyStruct{I: 1}, SmallStruct{I: 2, J: 3}, MidStruct{I: 4, J: 5, K: 6}, BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
|
|
FuncStructPtrParams(&TinyStruct{I: 1}, &SmallStruct{I: 2, J: 3}, &MidStruct{I: 4, J: 5, K: 6}, &BigStruct{I: 7, J: 8, K: 9, L: 10, M: 11, N: 12, O: 13, P: 14, Q: 15, R: 16})
|
|
i := 100
|
|
s := StructWithAllTypeFields{
|
|
i8: 1,
|
|
i16: 2,
|
|
i32: 3,
|
|
i64: 4,
|
|
i: 5,
|
|
u8: 6,
|
|
u16: 7,
|
|
u32: 8,
|
|
u64: 9,
|
|
u: 10,
|
|
f32: 11,
|
|
f64: 12,
|
|
b: true,
|
|
c64: 13 + 14i,
|
|
c128: 15 + 16i,
|
|
slice: []int{21, 22, 23},
|
|
arr: [3]int{24, 25, 26},
|
|
arr2: [3]E{{i: 27}, {i: 28}, {i: 29}},
|
|
s: "hello",
|
|
e: E{i: 30},
|
|
pf: &StructWithAllTypeFields{},
|
|
pi: &i,
|
|
intr: &Struct{},
|
|
m: map[string]uint64{"a": 31, "b": 32},
|
|
c: make(chan int),
|
|
err: errors.New("Test error"),
|
|
fn: func(s string) (int, error) {
|
|
println("fn:", s)
|
|
i = 201
|
|
return 1, errors.New("fn error")
|
|
},
|
|
pad1: 100,
|
|
pad2: 200,
|
|
}
|
|
globalStructPtr = &s
|
|
globalStruct = s
|
|
println("globalInt:", globalInt)
|
|
println("s:", &s)
|
|
FuncWithAllTypeStructParam(s)
|
|
println("called function with struct")
|
|
i, err := FuncWithAllTypeParams(
|
|
s.i8, s.i16, s.i32, s.i64, s.i, s.u8, s.u16, s.u32, s.u64, s.u,
|
|
s.f32, s.f64, s.b,
|
|
s.c64, s.c128,
|
|
s.slice, s.arr, s.arr2,
|
|
s.s,
|
|
s.e, s,
|
|
s.pf, s.pi,
|
|
s.intr,
|
|
s.m,
|
|
s.c,
|
|
s.err,
|
|
s.fn,
|
|
)
|
|
println(i, err)
|
|
println("called function with types")
|
|
println(globalStructPtr)
|
|
println(&globalStruct)
|
|
// Expected(skip):
|
|
// all variables: globalInt globalStruct globalStructPtr s i err
|
|
// s.i8: '\x01'
|
|
// s.i16: 2
|
|
s.i8 = 0x12
|
|
println(s.i8)
|
|
// Expected(skip):
|
|
// all variables: globalInt globalStruct globalStructPtr s i err
|
|
// s.i8: '\x12'
|
|
// globalStruct.i8: '\x01'
|
|
println((*globalStructPtr).i8)
|
|
println("done")
|
|
println("")
|
|
println(&s, &globalStruct, globalStructPtr.i16, globalStructPtr)
|
|
globalStructPtr = nil
|
|
}
|
|
|
|
var globalInt int = 301
|
|
var globalStruct StructWithAllTypeFields
|
|
var globalStructPtr *StructWithAllTypeFields
|