In my code I have a function that converts a string to sql.NullString
func NewNullString(s string) sql.NullString {
if len(s) == 0 {
return sql.NullString{}
}
return sql.NullString{
String: s,
Valid: true,
}
}
Then whenever I am using Exec I wrap my strings that could be NULL in the DB with the NewNullString function.
db.Exec(`
insert into
users first_name, last_name, email
values (?,?,?)`,
firstName,
lastName,
NewNullString(email),
)