Nil Interfaces
What will be printed when the code below is executed?And explain why println(InitType() == nil)
cannot be compiled?
package main
type S struct{}
func (s S) F() {}
type IF interface {
F()
}
func InitType() S {
var s S
return s
}
func InitPointer() *S {
var s *S
return s
}
func InitEfaceType() interface{} {
var s S
return s
}
func InitEfacePointer() interface{} {
var s *S
return s
}
func InitIfaceType() IF {
var s S
return s
}
func InitIfacePointer() IF {
var s *S
return s
}
func main() {
//println(InitType() == nil)
println(InitPointer() == nil)
println(InitEfaceType() == nil)
println(InitEfacePointer() == nil)
println(InitIfaceType() == nil)
println(InitIfacePointer() == nil)
}
Answer
true
false
false
false
false