“无”在“哥朗”里是什么意思?

使用 没有的案例很多,例如:

func (u *URL) Parse(ref string) (*URL, error) {
refurl, err := Parse(ref)
if err != nil {
return nil, err
}
return u.ResolveReference(refurl), nil
}

但我们不能这样使用它:

var str string //or var str int
str = nil

Golang 编译器将抛出一个 can't use nil as type string in assignment错误。

看起来 nil只能用于结构和接口的指针。如果是这样的话,那么 < strong > 是什么意思呢? 和 当我们用它来比较其他物体时,它们是如何比较的,换句话说,Golang 是如何确定一个物体为零的

编辑: 例如,如果一个接口为 nil,那么它的类型和值必须同时为 nil。Golang 是怎么做到的?

80220 次浏览

nil in Go is simply the NULL pointer value of other languages.

You can effectively use it in place of any pointer or interface (interfaces are somewhat pointers).

You can use it as an error, because the error type is an interface.

You can't use it as a string because in Go, a string is a value.

nil is untyped in Go, meaning you can't do that:

var n = nil

Because here you lack a type for n. However, you can do

var n *Foo = nil

Note that nil being the zero value of pointers and interfaces, uninitialized pointers and interfaces will be nil.

In Go, nil is the zero value for pointers, interfaces, maps, slices, channels and function types, representing an uninitialized value.

nil doesn't mean some "undefined" state, it's a proper value in itself. An object in Go is nil simply if and only if it's value is nil, which it can only be if it's of one of the aforementioned types.

An error is an interface, so nil is a valid value for one, unlike for a string. For obvious reasons a nil error represents no error.

  • For the data types like slice and pointers which "refer" to other types, "nil" indicates that the variable does not refer to an instance (equivalent of null pointer in other languages)
  • It is thee zero value of the variables of type functions, maps, interfaces, channels and structures

nil in Go means a zero value for pointers, interfaces, maps, slices, and channels. It means the value is uninitialized

In Golang, datatype can use nil is pointers, interfaces, maps, slices, channels, function types and errors

in String value is ""

in INT value is ZERO

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

nil is also a value but only difference is- it is empty.

In Javascript for the un-initialized variable will be undefined. In the same way Golang has nil as default value for all the un-initalized data types.

In Go nil is predeclared identifier that represents nothing for pointers, interfaces, channels, maps, slices and function types. Uninitialized pointers and interfaces will be nil. A string doesn’t qualify to be either of the above-mentioned types and hence can’t be assigned nil. If you declare a variable by shorthand decleration like this: variable:=nil this will give compile time error because compiler has no idea which type it has to assign to variable .

In Go map, slice and function doesn't support comparison but can be compared with nil. If any two map, slice or function are assigned to nil and compared with each other then it will raise runtime error. But if we assign nil to any two pointer variables and compare them with each other then no error will occur.

package main


import (
"fmt"
)


func main() {
var i *int =nil
var j *int =nil
if i==j {
fmt.Println("equal")
} else {
fmt.Println("no")
}
}

This code works fine and prints output as equal.

In Golang, zero value is the default value for defined variables. The default value of different datatypes are:

  • 0 for integer types
  • 0.0 for floating types
  • false for boolean types
  • "" for string types
  • nil for interfaces, slices, channels, maps, pointers and functions

So, nil is the zero value or default value for interfaces, slices, channels, maps, pointers and functions.

For example,

var a int
var s []int
var s1 []int = []int{1, 2}


fmt.Println(a == nil) // throw error
// invalid operation: a == nil (mismatched types int and untyped nil)


fmt.Println(s == nil) // true
fmt.Println(s1 == nil) // false

Since error is an interface, we can write

_, err := Parse("")
if err != nil { // if there is an error
// handle error
}