22
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

struct にアノテーションつけてたら go vet . すべき

Last updated at Posted at 2015-09-24

TL;DR
struct にアノテーションつけてたら go vet . すべき
go build, go install などは何もチェックしてくれない

下記のように間違ったフォーマットのアノテーションでも go build はなにも言ってくれないが

type WhiteList struct {
	Id      int        `db:"pk" column:"id"`
	UserId  int        `db:"unique" column:"user_id"`
	Count   int        `default:0 column:"cnt"`
	EndFlag bool       `default:0 column:"end"`
	AddedAt *time.Time `column:"added_at"`
}

go vet 様なら

(master)$go vet .
database.go:21: struct field tag `default:0` not compatible with reflect.StructTag.Get: bad syntax for struct tag value
database.go:22: struct field tag `default:0` not compatible with reflect.StructTag.Get: bad syntax for struct tag value

正しくはこうだった

type WhiteList struct {
	Id      int        `db:"pk" column:"id"`
	UserId  int        `db:"unique" column:"user_id"`
	Count   int        `default:"0" column:"cnt"`
	EndFlag bool       `default:"0" column:"end"`
	AddedAt *time.Time `column:"added_at"`
}

go の struct アノテーションはフォーマットが間違ってると column:"cnt" とかの合ってる部分もまるまる読み込んでくれないうえに実行時エラーも(当然)なく動くので、この場合 column は Count, EndFlag でつくられちゃってた。

22
12
1

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
22
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?