My Confusion is shown in the following code snippet:
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
What puzzles me most is that both c := []Ex{{1, 2}, {3, 4}}
and d := []*Ex{{1, 2}, {3, 4}}
can work well. Why can{1, 2}
be used to initialize both an object and a pointer?
I found the same question and someone answered:
If is a pointer like *Ex or *Track, it also automatically initializes correctly:
But I expect a deeper explanation. Is there any official documentation on this question?
I have a good C/C++ foundation, but a Golang novice. Looking forward to your answers, and I thank you in advance.