mcnulty is a real downer. No matter what sort of type you give McNulty, ol' McNulty always returns a zero value that is appropriate for that type.
Why?!
Two reasons:
As I work more and more with generic, I keep running into comparison issues with the standard blah == nil pattern. Because, you know, you might receive a non-nilable thingy.
I never met a pun I didn't want to take to dinner.
I like incredibly small modules that provide value.
I can't count.
Installation
Go 1.18+ required, as mcnulty uses generics.
go get -u codeberg.org/ess/mcnulty
Usage
package main
import "mcnulty"
func main() {
var x int
// use your imagination and assume that we do something that manipulates x
if x == mcnulty.Nil[int]() {
panic("McNulty says no!")
}
}
Nil returns the zero value of the given type.
For a pointer type, it returns nil.
For a struct type, it returns a struct with all fields set to their zero value.
For a slice type, it returns nil.
For a map type, it returns nil.
For a channel type, it returns nil.
For a function type, it returns nil.
For an interface type, it returns nil.
For a string type, it returns "".
For a numeric type, it returns 0.
For a boolean type, it returns false.
For a complex type, it returns 0i.