1. map一定要初始化,slice可以不用初始化
2. pointer一定要初始化
var c *Course => must init => c = &Course{} or new(Course)
3. for-loop的临时变量是一个值会变化的变量
var out []*int for i:= 0; i<3; i++{ // 保存的是i的地址,但是i最后会变成3,slice里全部是3 out = append(out,&i) }
=> tmp := i
out = append(out,&tmp)
// 1. use tmp
for _,id := range goodsId{ tmp := id go func(){ 输出tmp }() }
// 2. use pass by value
go func(id uint64){ print id }(id)
4. generics
func Add[T int|int32|float32|float64|uint64](a,b T) T{ return a+b }
Add[int](1,2)
substitution:interface{} func IAdd(a,b interface{}) interface{}{ switch a.(type){ case int: return a.(int) + b.(int) case int32: return ... } return nil } IAdd(1,2).(int)
标签:tmp,return,int,mistakes,func,go,id From: https://www.cnblogs.com/sabertobih/p/18261908