Have to know the Golang knowledge point nil

Source: Internet
Author: User
Tags builtin comparable
This is a creation in Article, where the information may have evolved or changed. The ' nil ' in Golang, a lot of people mistakenly think of Java, PHP and other programming languages in the same way as null. But actually Golang's niu is much more complicated, and if not, let's continue reading. ' Nil ' is a pre-declared identifier, defined in ' 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 Typevar ni L 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 ' ' # # NI The 0 value of L is according to the Go Language specification, and any type has a value of 0 when uninitialized: The Boolean type is False, the integer is 0, the string is "", and the 0 values for pointers, functions, interface, slice, channel, and map are nil. **ps: This does not say that the struct struct's 0 value is nil, because the struct's 0 value is related to its properties * * ' nil ' has no default type, although it is a 0 value of more than one type, you must explicitly or implicitly specify the explicit type of each nil usage. ' Package MainFunc Main () {//clear. _ = (*struct{}) (nil) _ = []int (nil) _ = Map[int]bool (nil) _ = Chan String (nil) _ = (func ()) ( Nil) _ = interface{} (nil)//implicit. var _ *struct{} = Nilvar _ []int = Nilvar _ Map[int]bool = Nilvar _ Chan string = Nilvar _ F UNC () = Nilvar _ interface{}= Nil} "If you follow the Golang keyword, you will find that there is no ' nil ', that is, ' nil ' is not a keyword, then you can define ' nil ' in the code, then ' nil ' will be hidden. ' Package Mainimport ' FMT ' func main () {nil: = 123fmt. Println (nil)//123var _ Map[string]int = nil//cannot use nil (type int) as type Map[string]int in Assignment} ' ' # nil type The memory layout of all values of the address and value size ' nil ' type is always the same, in other words: the memory address of the different type ' nil ' is the same. ' Package Mainimport ("FMT") func main () {var m map[int]stringvar ptr *intvar sl []intfmt. Printf ("%p\n", m)//0x0fmt. Printf ("%p\n", PTR)//0x0fmt. Printf ("%p\n", SL)//0x0} "business generally represents ' nil ' value as an exception. The nil value is always the same size as the ' non-nil ' value with the same type as the ' nil ' value. Therefore, a nil identifier that represents a different 0 value may have a different size. ' Package Mainimport ("FMT" "unsafe") Func main () {var p *struct{} = nilfmt. Println (unsafe. Sizeof (P))//8var s []int = Nilfmt. Println (unsafe. Sizeof (s))//24var m map[int]bool = nilfmt. Println (unsafe. Sizeof (m))//8var c chan string = nilfmt. Println (unsafe. Sizeof (c))//8var f func () = Nilfmt. Println (unsafe. Sizeof (f))//8var i interface{} = nilfmt. Println (unsafe. Sizeof (i))//16} "size is the compiler and architectureDependent. The above print results are 64-bit architecture and the official Go compiler. For a 32-bit architecture, the size of the print will be half. For the official Go compiler, the same kind of different types of two nil values are always the same size. For example, the two nil values for two different tile types ([]int and []string] are always the same. # # Nil Value comparison 1. Different types of ' nil ' are not comparable. ' Package Mainimport ("FMT") func main () {var m map[int]stringvar ptr *intfmt. Printf (M = = ptr)//invalid operation:m = = PTR (mismatched types map[int]string and *int)} "in Go, two values of two different comparable types can only be in one value The comparison is made implicitly when converting to another type. Specifically, there are two cases where two different values can be compared:-the type of one of the two values is the underlying type of the other. -a type of one of two values implements the type of another value (must be an interface type). * * the ' nil ' value comparison does not deviate from the above rules. * * ' Package mainimport ("FMT") func main () {type IntPtr *intfmt. Println (IntPtr (nil) = = (*int) (nil))//truefmt. Println ((interface{}) (nil) = = (*int) (nil))//false} ' 2. Two ' nil ' values of the same type may not be compared because the presence of map, Golang, and function types in slice is not a comparable type. They have a nickname of ' incomparable type ', so it is also illegal to compare their ' nil '. ' Package Mainimport ("FMT") func main () {var v1 []int = Nilvar v2 []int = nilfmt. Println (v1 = = v2) fmt. Println ((Map[string]int) (nil) = = (Map[string]int) (nil)) FMT. Println (func ()) (nil) = = (Func ()) (nil)} "the value of an incomparable type" is comparable to "pure nil". ' Package Mainimport ("FMT") func main () {FMt. Println ((Map[string]int) (nil) = = nil)//truefmt. Println ((func ()) (nil) = = nil)//true} ' 3. Two ' nil ' value may not be equal if one of the two compared nil values is an interface value and the other is not, assuming they are comparable, the comparison result is always false. The reason is that the interface value is converted to the type of the interface value before the comparison is made. The converted interface value has a specific dynamic type, but the other interface values are not. That's why the comparison is always wrong. ' Package Mainimport ("FMT") func main () {FMT. Println ((interface{}) (nil) = = (*int) (nil)//False} ' # # FAQ 1. Function return ' func Nilreturn () (string,error) {return nil,nil/ /cannot use of nil as type string in return argument} ' because ' ERROR ' is an interface type so ' error ' type has no error. The 2.map nil keymap key is a pointer, function, interface, slice, channel, and map, then key can be nil. ' Package Mainimport ("FMT") func main () {mmap: = Make (map[*string]int,4) a:= "a" mmap[&a] = 1mmap[nil] = 99fmt. Println (mmap)//map[0xc042008220:1 <nil>:99]} "# # Summary nil is more difficult to understand because we often confuse the nil value and nil type, I hope you students savor the difference. 416 reads  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.