▲ 4 ▼ Working effectively with database nulls
This post covers how to marshall null values from a database into a Go struct type and how to save nulls back to the database. I’ll cover the standard library’s sql.NullString, NullInt64, NullFloat64, etc types — which I’ll refer to as sql.NullXYZ when indicating the collection of types and not a specific Null type — important methods on those types, telltale errors, and some helpers for working with values coming from form posts.
Register to comment or vote on this story
Nulls from the DB make me wish go didn’t have nil at all - they cause bugs but don’t give you a lot of info. I’ve so far ignored and replaced with zero values on every query without any ill effects for new systems. It’s surprisingly rare to need a null value as opposed to zero value.