Skip to content

Instantly share code, notes, and snippets.

@robdaemon
Created September 5, 2018 22:32
Show Gist options
  • Save robdaemon/71629b4e573f37f488b4a297e976273a to your computer and use it in GitHub Desktop.
Save robdaemon/71629b4e573f37f488b4a297e976273a to your computer and use it in GitHub Desktop.
nil handling may surprise you
package main
import (
"fmt"
"reflect"
)
func main() {
var myint interface{}
myint = (*int64)(nil)
if myint == nil {
fmt.Println("it's nil")
} else {
fmt.Println("it's not nil")
}
v := reflect.ValueOf(myint)
if v.Kind() == reflect.Ptr && v.IsNil() {
fmt.Println("it's nil via reflection")
} else {
fmt.Println("it's not nil via reflection")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment