Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang declaring variables as NULLs

Tags:

go

As per the example below, I need to get email and firstname and dateofbirth and so on as NullStrings and NullTimes as required, as my User struct is using them. How do I declare variables as NULLs

package entities

import (
    "database/sql"
    "github.com/go-sql-driver/mysql"
    "testing"
    "time"
)

var (
    email          = sql.NullString("[email protected]") << Does not work
    hashedPassword = "password"
    firstName      = "Lee"
    lastName       = "Brooks"
    dateOfBirth    = time.Now
    height         = 1.85
    weight         = 101.3
)

func privacyConcernedUser() *User {
    return &User{
        Email:          email, << These all complain eg: cannot use Email (type string) as type sql.NullString in field value
        HashedPassword: hashedPassword,
        FirstName:      firstName,
        LastName:       lastName,
    }
}
like image 388
Lee Avatar asked Jan 17 '14 01:01

Lee


People also ask

How to declare multiple variables in Golang?

The variable type gets inferred by the Go compiler after the declaration happens so it cannot be reassigned with a new type. Multiple variables can be declared using the shorthand syntax of variable declaration.

How do you express a null value in Golang?

How do you express a "null" value in Go? The equivalent of NULL is nil, as you already discovered. Note, though, that you don't generally need to initialize things to nil or zero in Go, because by default all variables (including dynamically allocated ones) are set to “zero values” according to type (numbers zero, references nil ).

What is short variable declaration operator (=) in Golang?

Short Variable Declaration Operator (:=) in Go Last Updated : 17 May, 2021 Short Variable Declaration Operator (:=) in Golang is used to create the variables having a proper name and initial value. The main purpose of using this operator to declare and initialize the local variables inside the functions and to narrowing the scope of the variables.

What is the blank identifier in Golang?

The blank identifier is the single underscore (_) operator. It is used to ignore the values returned by functions or import for side-effects. Why is it needed? Go compiler throws an error whenever it encounters a variable declared but not used. Now we can simply use the blank identifier and not declare any variable at all.


2 Answers

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

like image 195
az_ Avatar answered Sep 22 '22 22:09

az_


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

Try:

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

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

like image 20
mattes Avatar answered Sep 20 '22 22:09

mattes