Golang declaring variables as NULLs

sql.NullString isn't a drop-in replacement for the string type, you have to some work with it.

package main

import "database/sql"
import "fmt"

type User struct {
    Name string
}

func main() {
    var nsName sql.NullString
    if err := nsName.Scan("User's Name"); err != nil {
        panic(err)
    }
    user := &User{Name: nsName.String}
    fmt.Println(user)
}

You can check if the NullString is valid with nsName.Valid.

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


sql.NullString("[email protected]") << Does not work

Try:

sql.NullString{"[email protected]", true}

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

Tags:

Go