ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
* slice, map, channel, pointer, func, interface 零值为 nil ~~~ package main import "fmt" //* 如果变量没有显式初始化,则被隐式地赋予其类型的零值(zero value),数值类型是0,字符串类型是空字符串"" //slice, map, channel, pointer, func, interface 零值为 nil var field1 []int var field2 map[int]string var field3 chan int var field4 *int var field5 func() var field6 interface{} func main() { if field1 == nil { fmt.Println("field1 zero value is nil") } if field2 == nil { fmt.Println("field2 zero value is nil") } if field3 == nil { fmt.Println("field3 zero value is nil") } if field4 == nil { fmt.Println("field4 zero value is nil") } if field5 == nil { fmt.Println("field5 zero value is nil") } if field6 == nil { fmt.Println("field6 zero value is nil") } println(field1) println(field2) println(field3) println(field4) println(field5) println(field6) } ~~~ * 打印结果 ~~~ num1 zero value is nil num2 zero value is nil num3 zero value is nil num4 zero value is nil num5 zero value is nil num6 zero value is nil [0/0]0x0 0x0 0x0 0x0 0x0 (0x0,0x0) ~~~ * error的零值也是nil, 为什么上面没说到? ~~~ package main import "fmt" var err error func main() { fmt.Println(err) } ~~~ * 打印结果 ~~~ <nil> ~~~ * 因为error不是基本类型, 是自定义类型, 底层是interface ![](/D:/%E6%AD%A3%E5%BC%8F%E6%A1%8C%E9%9D%A2/Golang%E5%B7%A5%E4%BD%9C%E7%AC%94%E8%AE%B0/book/Golang%E5%B7%A5%E4%BD%9C%E7%AC%94%E8%AE%B0/images/QQ%E6%88%AA%E5%9B%BE20190124172114.jpg)