golang nil

不得不知道的golang知识点之nil

僤鯓⒐⒋嵵緔 提交于 2019-12-02 22:46:02
golang中的 nil ,很多人都误以为与Java、PHP等编程语言中的null一样。但是实际上Golang的niu复杂得多了,如果不信,那我们继续往下阅读。 nil 为预声明的标示符,定义在 builtin/builtin.go , // nil is a predeclared identifier representing the zero value for a // pointer, channel, func, interface, map, or slice type. // Type must be a pointer, channel, func, interface, map, or slice type var nil Type // Type is here for the purposes of documentation only. It is a stand-in // for any Go type, but represents the same type for any given function // invocation. type Type int nil的零值 按照Go语言规范,任何类型在未初始化时都对应一个零值:布尔类型是false,整型是0,字符串是"",而指针、函数、interface、slice