This question already has an answer here:
I have this small golang test I cannot understand:
package main
import "fmt"
type myObj struct {
}
func nilObj() *myObj {
return nil
}
func nilInt() interface{} {
return nil
}
func main() {
var obj1 interface{}
fmt.Println(obj1 == nil) // true
obj1 = nilObj()
fmt.Println(obj1 == nil) // false
var obj2 *myObj
fmt.Println(obj2 == nil) // true
obj2 = nilObj()
fmt.Println(obj2 == nil) // true
var obj3 interface{}
fmt.Println(obj3 == nil) // true
obj3 = nilInt()
fmt.Println(obj3 == nil) // true
}
Between obj1 and obj2, only the variable declaration changes, but the result is different.
Between obj1 and obj3, the function call does not return the same type (struct pointer vs interface). I am not entirely sure I understand the result.
Any help is welcome (https://play.golang.org/p/JcjsJ-_S8I)
</div>