go struct结构体转换为slice切片


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)

 

go struct结构体转换为slice切片

上一篇:Proxmox VE + Ceph 超融合项目实战(第五部分:虚拟网络)


下一篇:List、Set和Map的区别