Documentation ¶
Overview ¶
Package is provides some assertions on reflect.Values and reflect.Types.
Go's reflect package can sometimes leave one asking basic questions, like "how do I know if a value is a zero value", or "is this field exported?". Although there are well-documented ways to answer these kinds of questions, discoverability can be hard.
I hope this package comes in handy for quick and dirty assertions, but would encourage you to use this code as inspiration to writing your own reflect code more accurately and efficiently.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Zero ¶
func Zero(v interface{}) bool
Zero returns true when the underlying value of v is either nil or zero for its underlying type.
Example (False) ¶
a := "a" b := 1 c := &b fmt.Println("a:", Zero(a), "b:", Zero(b), "c:", Zero(c))
Output: a: false b: false c: false
Example (True) ¶
a := "" b := 0 c := ((*int)(nil)) fmt.Println("a:", Zero(a), "b:", Zero(b), "c:", Zero(c))
Output: a: true b: true c: true
func ZeroForType ¶
ZeroForType returns true when v matches the zero value of typ. If typ is nil, always returns false.
Example (False) ¶
// i is the type interface{} i := reflect.TypeOf((*interface{})(nil)).Elem() fmt.Println( "a:", ZeroForType("a", i), "b:", ZeroForType(1, i), "c:", ZeroForType(i, i), "d:", ZeroForType(1, nil), )
Output: a: false b: false c: false d: false
Example (True) ¶
// i is the type interface{} i := reflect.TypeOf((*interface{})(nil)).Elem() fmt.Println( "a:", ZeroForType("", reflect.TypeOf("a")), "b:", ZeroForType(0, reflect.TypeOf(1)), "c:", ZeroForType(nil, reflect.TypeOf(&i)), "d:", ZeroForType(nil, reflect.TypeOf(i)), )
Output: a: true b: true c: true d: true
Types ¶
This section is empty.