type DataInLine struct {
Id string
Name string
Age string
}
func StrctToSlice(f DataInLine) []string {
v := reflect.ValueOf(f)
ss := make([]string, v.NumField())
for i := range ss {
ss[i] = fmt.Sprintf("%v", v.Field(i))
}
return ss
}
func main() {
ff:=DataInLine{
Id: "1" ,
Name: "zhangsan",
Age : "18" ,
}
qie:=StrctToSlice(ff)
fmt.Println("打印出来为切片:",qie)
}
结果:
打印出来为切片: [1 zhangsan 18]
Process finished with exit code 0