Golang database/sql.NullString type example

package database/sql

NullString represents a string that may be null. NullString implements the Scanner interface so it can be used as a scan destination:

Golang database/sql.NullString type usage example

Example 1: ( from http://golang.org/pkg/database/sql/#NullString )

 var s NullString
 err := db.QueryRow("SELECT name FROM foo WHERE id=?", id).Scan(&s)
 ...
 if s.Valid {
 // use s.String
 } else {
 // NULL value
 }

Example 2:

 var num int
 var text string
 var blob []byte
 var nothing sql.NullString

 err = txn.QueryRow("SELECT * FROM temp").Scan(&num, &text, &blob, &nothing)
 if err != nil {
 t.Fatal(err)
 }

Reference :

http://golang.org/pkg/database/sql/#NullString

Advertisement