Created
November 3, 2017 13:26
-
-
Save udhos/a0117af7cabc1bcebc3c9be7c7d78c31 to your computer and use it in GitHub Desktop.
Understanding Go nil interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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