Nil slice when transferring as an interface is not zero! What for? (Golang)

See this site: http://play.golang.org/p/nWHmlw1W01

package main import "fmt" func main() { var i []int = nil yes(i) // output: true no(i) // output: false } func yes(thing []int) { fmt.Println(thing == nil) } func no(thing interface{}) { fmt.Println(thing == nil) } 

Why is there a difference in output between two functions?

+7
null go
source share
1 answer

Admittedly, this is a bit of a quirk, but there is an explanation.

Imagine the interface{} variable as a structure consisting of two fields: one is a type and the other is data. ( []int and nil ). In fact, it looks the same as in Go runtime.

 struct Iface { Itab* tab; void* data; }; 

When you pass your nil fragment to yes , only nil is passed as the value, so your comparison comes down to nil == nil .

Meanwhile, a call to no automatically transfers your variable to interface{} , and the call becomes something like no(interface{[]int, nil}) . Thus, a comparison in no can be considered as interface{[]int, nil} == nil , which turns out to be false in go.

The problem is really explained in Go FAQs .

+11
source share

All Articles