Add check.IsNul to catch stupid nil interface err

pull/53/head
rwxrob 2 years ago
parent 70aec47e6b
commit bae4731293
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -1,5 +1,7 @@
package check
import "reflect"
// Blank checks that the passed string, []byte (or slice of either)
// contains something besides an empty string.
func Blank(i interface{}) bool {
@ -19,3 +21,10 @@ func Blank(i interface{}) bool {
}
return false
}
// IsNil is a shortcut for reflect.ValueOf(foo).IsNil() and should only
// be used when foo == nil is in question, such as whenever the value of
// foo is an interface of any kind. In fact, every interface should use
// this check instead just to be sure to avoid surprise (and extremely
// odd) logic errors. Nil is not "nil" in Go.
func IsNil(i interface{}) bool { return reflect.ValueOf(i).IsNil() }

@ -2,6 +2,7 @@ package check_test
import (
"fmt"
"reflect"
"github.com/rwxrob/bonzai/check"
)
@ -25,3 +26,18 @@ func ExampleBlank() {
// true
// true
}
func ExampleIsNil() {
var names []string
var namesi interface{}
namesi = names
fmt.Println(names == nil)
fmt.Println(namesi == nil)
fmt.Println(reflect.ValueOf(namesi).IsNil())
fmt.Println(check.IsNil(namesi))
// Output:
// true
// false
// true
// true
}

Loading…
Cancel
Save