Why does Go have typed nil
? It throws an explicit interface conformation check for convenience. What's the problem of untyped nil
and what did the designers want to solve with typed nil
?
In the Go programming language, nil is a zero value. Recall from unit 2 that an integer declared without a value will default to 0. An empty string is the zero value for strings, and so on. A pointer with nowhere to point has the value nil .
Nil represents a zero value in Golang. As you may already know, zero values are the "default" value for defined variables in Go. This means that some types doesn't cannot hold empty values, instead acquiring zero values upon initialization to get rid of the problem of checking values for emptyness.
nil s in Go. nil is a frequently used and important predeclared identifier in Go. It is the literal representation of zero values of many kinds of types. Many new Go programmers with experiences of some other popular languages may view nil as the counterpart of null (or NULL ) in other languages.
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.
It sounds like you're asking about this error message:
http://play.golang.org/p/h80rmDYCTI
package main import "fmt" type A struct {} type B struct {} func (a *A) Foo() { fmt.Println("A") } func (b *B) Foo() { fmt.Println("B") } func main() { n := nil n.Foo() }
This prints:
prog.go:17: use of untyped nil [process exited with non-zero status]
In that example, should the program print "A" or "B"?
You have to help the compiler decide. The way you do that is by specifying the type of n
.
For example:
http://play.golang.org/p/zMxUFYgxpy
func main() { var n *A n.Foo() }
prints "A".
In other languages, n.Foo()
might crash immediately if n
is nil
or its equivalent. Go's language designers decided to let you determine what should happen instead. If you access the pointer without checking for nil
, you get the same behavior as in other languages.
This is due to type safety. nil
is actually the value of uninitialized variables in Go. The nil
values for slices, maps, functions, channels, pointers and interfaces are not the same type, and not comparable. See The language spec for more details.
EDIT: As pointed out by @newacct the correct technical term for this is the "zero value" for the type:
When memory is allocated to store a value, either through a declaration or a call of make or new, and no explicit initialization is provided, the memory is given a default initialization. Each element of such a value is set to the zero value for its type: false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.
Playground example
There is also some information regarding nil interfaces and errors at Why is my nil error value not equal to nil? in the Go FAQ.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With