package main
import (
"reflect"
"strconv"
)
type Test struct {
Field1 string
Field2 string
Field3 int
}
func main() {
st := Test{"value1", "value2", 1234}
v := reflect.Indirect(reflect.ValueOf(st))
t := v.Type()
for i := 0; i < t.NumField(); i++ {
// フィールド名
println("Field: " + t.Field(i).Name)
// 値
f := v.Field(i)
i := f.Interface()
if value, ok := i.(int); ok {
println("Value: " + strconv.Itoa(value))
} else {
println("Value: " + f.String())
}
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
List of users who liked
2016