Skip to content

Instantly share code, notes, and snippets.

@udhos
Created November 3, 2017 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save udhos/a0117af7cabc1bcebc3c9be7c7d78c31 to your computer and use it in GitHub Desktop.
Save udhos/a0117af7cabc1bcebc3c9be7c7d78c31 to your computer and use it in GitHub Desktop.
Understanding Go nil interface
package main
import (
"fmt"
)
type T struct {
}
func main() {
var v *T // v is a nil pointer
var v2 interface{} // v2 is a nil interface (holds nothing)
v2 = v // v2 is a non-nil interface holding a nil pointer (v)
fmt.Printf("v is a nil pointer: %v\n", v == nil)
fmt.Printf("v2 is a nil interface: %v\n", v2 == nil)
fmt.Printf("v2 is a interface holding a nil pointer: %v\n", v2 == (*T)(nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment