3 ms·
Here's a four-line replacement for the various `sql.Null*` types in the standard library. type Null(type T) struct { Val T Valid bool //
by conroy 6y ago
Here's a four-line replacement for the various `sql.Null*` types in the standard library.
type Null(type T) struct {
Val T
Valid bool // Valid is true if Val is not NULL
}
Here's what it looks like in practice: https://go2goplay.golang.org/p/Qj8MqYWWAc3 https://go2goplay.golang.org/p/Qj8MqYWWAc3
- sethammons 6y agoneat example, thanks. It gives you a generic Null type that has a bool is-valid field. Very similar to the sql.Null{String,Int} types, but you don't have to declare each one. Kinda the point of generics :)
- tandr 6y agoNice. But I need to retrain myself to read Go code again, because func Make(type T)(v T) Null(T) { } looks really confusing and unreadable to me - too many brackets...
- IshKebab 6y agoI agree. I assume they avoided the standard <> brackets to keep things simple, but really it just makes it harder to parse visually.