Created
July 2, 2020 11:24
-
-
Save smallnest/5b789d52af64fb59d1a4d199fc03e696 to your computer and use it in GitHub Desktop.
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
```go | |
func isNil(i interface{}) bool { | |
return i == nil || reflect.ValueOf(i).IsNil() | |
} | |
``` | |
better: | |
```go | |
func isNilFixed(i interface{}) bool { | |
if i == nil { | |
return true | |
} | |
switch reflect.TypeOf(i).Kind() { | |
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice: | |
return reflect.ValueOf(i).IsNil() | |
} | |
return false | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment