Golang将变量声明为NULLs。

huangapple go评论154阅读模式
英文:

Golang declaring variables as NULLs

问题

根据下面的示例,我需要将email、firstname、dateofbirth等变量声明为NullStrings和NullTimes,因为我的User结构体正在使用它们。如何将变量声明为NULLs?

package entities

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

var (
	email          = sql.NullString{"mail@gmail.com", true}
	hashedPassword = "password"
	firstName      = "Lee"
	lastName       = "Brooks"
	dateOfBirth    = mysql.NullTime{Time: time.Now(), Valid: true}
	height         = 1.85
	weight         = 101.3
)

func privacyConcernedUser() *User {
	return &User{
		Email:          email,
		HashedPassword: hashedPassword,
		FirstName:      firstName,
		LastName:       lastName,
	}
}

你可以使用sql.NullStringmysql.NullTime类型来声明变量为NULLs。在sql.NullString中,你需要提供一个字符串和一个布尔值,表示该字符串是否为NULL。在mysql.NullTime中,你需要提供一个时间值和一个布尔值,表示该时间是否为NULL。

英文:

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("mail@gmail.com") << 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,
	}
}

答案1

得分: 12

sql.NullString不是string类型的直接替代品,你需要对它进行一些处理。

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)
}

你可以使用nsName.Valid来检查NullString是否有效。

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

英文:

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

答案2

得分: 7

sql.NullString("mail@gmail.com") << 不起作用

尝试:

sql.NullString{"mail@gmail.com", true}

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

英文:

sql.NullString(&quot;mail@gmail.com&quot;) &lt;&lt; Does not work

Try:

sql.NullString{&quot;mail@gmail.com&quot;, true}

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

huangapple
  • 本文由 发表于 2014年1月17日 09:49:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/21176459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定