반응형
고랭
-
golang reflect로 struct 필드 순회하기메모 및 기타 2022. 11. 22. 16:56
Code package main import ( "fmt" "reflect" ) type Student struct { Name string Grade int Age int } type Animal struct { Name string Kind string Size int Age int } func main() { s1 := &Student{} LoopObjectField(s1) a1 := &Animal{} LoopObjectField(a1) } func LoopObjectField(object interface{}) { fmt.Println(reflect.TypeOf(object)) e := reflect.ValueOf(object).Elem() fieldNum := e.NumField() for i ..