What does nil mean in golang?

后端 未结 7 1010
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-24 06:12

There are many cases using nil in golang. For example:

func (u *URL) Parse(ref string) (*URL, error) {
    refurl, err := Parse(ref)
    if          


        
相关标签:
7条回答
  • 2020-12-24 06:53

    nil is a predeclared identifier in Go that represents zero values for pointers, interfaces, channels, maps, slices and function types. nil being the zero value of pointers and interfaces, uninitialized pointers and interfaces will be nil.

    In Go, string can’t be nil. A string doesn’t qualify to be assigned nil.

    a := nil   // It will throw an error 'use of untyped nil'
    
    var a *int = nil  //It will work
    
    0 讨论(0)
提交回复
热议问题