概要
GoでStringやIntのNull許容型をguregu/null packageで扱うときの方法を示す。
使い方
import "github.com/guregu/null"
"github.com/guregu/null"
をimportする。
null.Bool
null.NewBool
で初期化する
bool := null.NewBool(true, true)
第1引数は値、第2引数はこのstructがnullかどうか
null.Int
null.NewInt
で初期化する
int := null.NewInt(1, true)
第1引数は値、第2引数はこのstructがnullかどうか
null.String
null.NewString
で初期化する
string := null.NewString("example text", true)
第1引数は値、第2引数はこのstructがnullかどうか
structで使うとき
type ExampleStruct struct {
fieldBool null.Bool
fieldString null.String
fieldInt null.Int
}