1. マスター・オブ・
reflectパッケージ II
2016/04/17(日)
@第3回 関西golang勉強会
The Go gopher was designed by Renee French.
The gopher stickers was made by Takuya Ueda.
Licensed under the Creative Commons 3.0
Attributions license.
参考:マスター・オブ・reflectパッケージ
Go研 Vol.8
8. 変数に値を入れる
基礎編
8
var n int
fmt.Println(n) // 0
vp := reflect.ValueOf(&n)
v := vp.Elem()
if v.CanSet() {
v.SetInt(100)
}
fmt.Println(n) // 100
http://play.golang.org/p/HkJPjQsP8o
9. 構造体を触る
基礎編
9
s := struct{
A string `k:"v"`; b int
}{"a", 1}
v := reflect.ValueOf(&s).Elem() println(v.
FieldByName("A").CanSet())
println(v.FieldByName("b").CanSet())
f1, ok := v.Type().FieldByName("A")
println(ok, f1.PkgPath, f1.Tag.Get("k"))
f2, _ := v.Type().FieldByName("b")
println(f2.PkgPath)
http://play.golang.org/p/NkwP3KSjDu