関数内に構造体を定義してインタフェースで戻してフィールドと値を出力した時のメモです。
package main
import (
"fmt"
"reflect"
)
// 自己紹介関数
func selfIntroduction() (i interface{}) {
// 個人情報構造体
type personalInfomation struct {
Name string
LiveIn string
Job string
}
r := new(personalInfomation)
r.Name = "sky_jokerxx"
r.LiveIn = "Tokyo"
r.Job = "office worker"
return r
}
func main() {
r := selfIntroduction()
v := reflect.Indirect(reflect.ValueOf(r))
t := v.Type()
for i := 0; i < t.NumField(); i++ {
fmt.Println(t.Field(i).Name + ": " + v.Field(i).String())
}
}
参考