📢 #GOLANG WARNING
— Francesc (@francesc) 2018年2月7日
go-bindata creator deleted their @github account and someone else created a new account with the same name.
There's no guarantees that the new user has good intentions, so if you're using the repository make sure you verify it first!https://t.co/bOhPYRnFvV
go-bindata の作者が GitHub アカウントを削除し、他の誰かが同じ名前のアカウントと go-bindata を作成しました。
— mattn (@mattn_jp) 2018年2月8日
現時点では新しいユーザが良い意味でアカウントを作ったという保証はないのでリポジトリを使用している場合は、まず確認する様にしてください。 #golanghttps://t.co/OVtSyI1mWj
最近こういうことがあった. というわけでもしgo-bindataを使っている場合にはgo-assetsあたりに乗り換えたほうがいい.
例えば静的ファイル等を http.FileServer
で配るだけなら,最初からgo-bindataではなくgo-assetsを使っている方が多いのではないだろうか.
そうではなく,わざわざgo-bindataを使っていたのは,ファイルの中身をパースしたかったりするからなのではないだろうか?
というわけでgo-assetsを使って,go-bindataと同じようにファイルの中身をパースしてみる.
Assetsを作る
go-assetsはともかく,とりあえずgo-assets-builderを入れよう.
$ go get -u github.com/jessevdk/go-assets-builder
で,generateコマンドを変更する.
//go:generate go-bindata -o=config/bindata.go -pkg=config -prefix=config/ config/settings.yml
こんなのを
//go:generate go-assets-builder --output=config/bindata.go -s="/config" -p=config config/settings.yml
こうする.
これで go generate
すると新しい config/bindata.go
ができあがる.
Assetsを参照する
go-bindataでは,このようになっているとする.
buf, err := Asset("settings.yml") if err != nil { panic(err) } m := make(map[interface{}]interface{}) err = yaml.Unmarshal(buf, &m) if err != nil { panic(err) }
こいつをgo-assetsを使ったパターンに書き換える.
file, err := Assets.Open("/settings.yml") if err != nil { panic(err) } by := new(bytes.Buffer) io.Copy(by, file) buf := by.Bytes() m := make(map[interface{}]interface{}) err = yaml.Unmarshal(buf, &m) if err != nil { panic(err) }
こんな感じになる.
Assets.Open
した http.File
は io.Reader
を持っているので,bytes.Buffer
に io.Copy
してやれば []byte
に変換できる.
あとは簡単で,普通に Unmarshal
できる.
jsonでもこれと同じ要領でいけるだろう.