LoginSignup
1
0

More than 3 years have passed since last update.

GoのNull許容型guregu/nullの使い方

Posted at

概要

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
}

サンプルコード

Go Playground

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0