结构的零值
Struct's zero value
示例代码如下:
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{}
的性质是一样的吧?
为什么要(正确)猜测 some documentation?
When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value.
Each element of such a variable or value is set to the zero value for its type:
false
for booleans,
0
for integers,
0.0
for floats,
""
for strings,
- and
nil
for pointers, functions, interfaces, slices, channels, and maps.
This initialization is done recursively, so for instance each element of an array of structs will have its fields zeroed if no value is specified.
请注意,无法将结构值设置为 nil
(但您可以将指向结构的指针的值设置为 nil
)。
展示@Denys Séguret 的一流答案。此类变量或值的每个元素都设置为其类型的零值 (https://golang.org/ref/spec#The_zero_value):
package main
import "fmt"
func main() {
// false for booleans,
var bl bool // false
//0 for numeric types,
var in int // 0
// "" for strings,
var st string // ""
// and nil for pointers, functions, interfaces, channels,
var pi *int // <nil>
var ps *string // <nil>
var fu func() // <nil> Go vet error.
var ir interface{} // <nil>
var ch chan string // <nil>
fmt.Println(bl, in, st, pi, ps, fu, ir, ch)
// slices, and maps.
var sl []int // true
var mp map[int]string // true
var pm *map[int]string // <nil>
fmt.Printf("%v %v %v\n", sl == nil, mp == nil, pm)
}
我不相信 top-voted 答案的措辞清楚地回答了这个问题,所以这里有一个更明确的解释:
"如果未指定值,数组或结构的元素将其字段清零。此初始化以递归方式完成:"
示例代码如下:
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{}
的性质是一样的吧?
为什么要(正确)猜测 some documentation?
When storage is allocated for a variable, either through a declaration or a call of new, or when a new value is created, either through a composite literal or a call of make, and no explicit initialization is provided, the variable or value is given a default value.
Each element of such a variable or value is set to the zero value for its type:
false
for booleans,0
for integers,0.0
for floats,""
for strings,- and
nil
for pointers, functions, interfaces, slices, channels, and maps.This initialization is done recursively, so for instance each element of an array of structs will have its fields zeroed if no value is specified.
请注意,无法将结构值设置为 nil
(但您可以将指向结构的指针的值设置为 nil
)。
展示@Denys Séguret 的一流答案。此类变量或值的每个元素都设置为其类型的零值 (https://golang.org/ref/spec#The_zero_value):
package main
import "fmt"
func main() {
// false for booleans,
var bl bool // false
//0 for numeric types,
var in int // 0
// "" for strings,
var st string // ""
// and nil for pointers, functions, interfaces, channels,
var pi *int // <nil>
var ps *string // <nil>
var fu func() // <nil> Go vet error.
var ir interface{} // <nil>
var ch chan string // <nil>
fmt.Println(bl, in, st, pi, ps, fu, ir, ch)
// slices, and maps.
var sl []int // true
var mp map[int]string // true
var pm *map[int]string // <nil>
fmt.Printf("%v %v %v\n", sl == nil, mp == nil, pm)
}
我不相信 top-voted 答案的措辞清楚地回答了这个问题,所以这里有一个更明确的解释:
"如果未指定值,数组或结构的元素将其字段清零。此初始化以递归方式完成:"