总之就是被 Go 的指针和对应初始化坑了无数次
#
在这里留个笔记
方法内要改变原值就要对指针取值再赋
#
1
2
3
4
5
6
7
8
9
10
11
| type Some struct {
A int
}
func (c *Some) Handle(){
// Wrong: only affect in the method
// c = &Some{}
// Right
*c = Some{}
}
|
结构体内全部都要初始化,并且 chan 还不会报错
#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| type Some struct {
A []int
B map[string]int
C chan struct{}
D struct {
E int
}
}
V := Some{
A: []int{}
...
}
V.C = make(chan struct{})
|
JSON 访问权限需要通过成员首字母大写获得
#
1
2
3
4
| type Some struct {
private int
Public int
}
|
break 还会作用在 for select switch 上
#
没错,go 不需要在 switch 中 break,但是break仍然会其作用
1
2
3
4
5
6
7
8
9
10
11
12
| for {
select {
case <-Done:
// On Done
// Wrong: break
// Right
return
default:
...
}
}
|
多处等待读取 ch 请使用 close
#
1
2
3
4
5
6
7
8
9
10
11
12
| Done:=make(chan struct{})
...
case <-Done:
...
case <-Done:
...
// Signal to stop
// Wrong: only get 1 signal
// Done<-struct{}{}
// Right
close(Done)
|
时空乱流记录