先来一个返回指针的测试,结果跟想象一样
type A map[int]string
type B struct {
A
c int
}
func main() {
b := B{make(A), 10}
NewB := func() *B {
return &b
}
c := NewB()
c.c = 100
c.A[1] = "3"
fmt.Println(b, c)
}
/* output
{map[1:3] 100} &{map[1:3] 100}
*/
再试试直接返回值,这下的输出和想象中不一样了
type A map[int]string
type B struct {
A
c int
}
func main() {
a := B{make(A), 10}
b := B{make(A), 10}
NewB := func() B {
return b
}
c := NewB()
c.c = 100
c.A[1] = "3"
// c[1]="3" invalid operation: c[1] (type B does not support indexing)
fmt.Println(a, b, c)
}
/* output
{map[] 10} {map[1:3] 10} {map[1:3] 100}
*/
为什么呢,再试试
type A map[int]string
type B struct {
A
c int
}
func main() {
a := make(A)
NewA := func() A {
return a
}
b := NewA()
b[1] = "10"
fmt.Println(a, b)
}
/* output
map[1:10] map[1:10]
*/
这样就编译不过了
func main() {
a := make(A)
NewA := func() *A {
return &a
}
b := NewA()
b[1] = "10" //invalid operation: b[1] (type *A does not support indexing)
fmt.Println(a, b)
}
go 语言中map,slice都是指针类型,以值的形式返回struct的时候只是把map对应的指针拷贝了
有疑问加站长微信联系(非本文作者)