这是示例代码:
package main
import (
"fmt"
)
type A struct {
Name string
}
func (this *A) demo(tag string) {
fmt.Printf("%#v\n", this)
fmt.Println(tag)
}
func main() {
var ele A
ele.demo("ele are called")
ele2 := A{}
ele2.demo("ele2 are called")
}
运行结果:
&main.A{Name:""}
ele are called
&main.A{Name:""}
ele2 are called
它看起来像那些是相同的约var ele A和ele2 := A{}
所以,结构体的零值不是nil,而是一个结构体,所有的属性都被初始化为零值。猜对了吗?
如果猜对了,那么性质var ele A和ele2 := A{}是一样的吧?
Qyouu
呼如林
相关分类