Created
September 5, 2018 22:32
-
-
Save robdaemon/71629b4e573f37f488b4a297e976273a to your computer and use it in GitHub Desktop.
nil handling may surprise you
This file contains hidden or 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" | |
"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