Files
the-way-to-go_ZH_CN/eBook/exercises/chapter_11/interface_nil.go
2015-03-03 12:25:25 -05:00

47 lines
1007 B
Go
Executable File

// interface_nil.go
package main
import "fmt"
type Any interface {}
type Anything struct {}
func main() {
any := getAny()
if any == nil {
fmt.Println("any is nil")
} else {
fmt.Println("any is not nil")
}
/*
// to get the inner value:
anything := any.(*Anything)
if anything == nil {
fmt.Println("anything is nil")
} else {
fmt.Println("anything is not nil")
}
*/
}
func getAny() Any {
return getAnything()
}
func getAnything() *Anything {
return nil
}
/* Output:
any is not nil
WHY?
you would perhaps expect: any is nil,because getAnything() returns that
BUT:
the interface value any is storing a value, so it is not nil.
It just so happens that the particular value it is storing is a nil pointer.
The any variable has a type, so it's not a nil interface,
rather an interface variable with type Any and concrete value (*Anything)(nil).
To get the inner value of any, use: anything := any.(*Anything)
now anything contains nil !
*/