i'm actually using "github.com/markbates/pop/nulls" because i need correct 
json
marshalling and unmarshalling.

but i was wondering: aside from above json requirements, and writing helper 
methods such as those discussed in

--------------------------------------------------------------------------------------------
https://marcesher.com/2014/10/13/go-working-effectively-with-database-nulls/

and 

https://stackoverflow.com/questions/40266633/golang-insert-null-into-sql-instead-of-empty-string

(from the latter stackoverflow discussion:
...
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),
)
...
)
----------------------------------------------------------------------------------

what else do you do to make coding easier?

do you maintain two versions of your struct, one where all fields are just 
their primitive int or string types, and the other with something like 
sql.NulllXYZ types?

say type Film struct {
       FilmID: int,
   Title: string,
   ReleaseYear: int
}

and
type ilmX struct {
       FilmID: int,
   Title: string,
   ReleaseYear: nulls.Int //db nullable
}
perhaps with additional "conversion" methods, such as: func FilmFrom(x 
*FilmX) (*Film, error) { ... } ??

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to