我的困惑显示在以下代码片段中:
type Ex struct{
A,B int
}
a := []Ex{Ex{1, 2}, Ex{3, 4}} //it works, and I understand it
b := []*Ex{&Ex{1, 2}, &Ex{3, 4}} //it works, and I understand it
c := []Ex{{1, 2}, {3, 4}} //it works, and I don't understand it
d := []*Ex{{1, 2}, {3, 4}} //it works, and I don't understand it
e := []*Ex{{1, 2}, &Ex{3, 4}} //it works, and I don't understand it
最让我困惑的是,两者都可以很好地工作。为什么可以同时用于初始化对象和指针?c := []Ex{{1, 2}, {3, 4}}d := []*Ex{{1, 2}, {3, 4}}{1, 2}
我发现了同样的问题,有人回答了:
如果 是像 *Ex 或 *Track 这样的指针,它也会自动正确初始化:
但我期待一个更深层次的解释。是否有关于这个问题的官方文件?
我有一个很好的C/C++基础,但是一个Golang新手。期待您的回答,我提前感谢您。
FFIVE
相关分类