假设我以这种方式设置了两种类似的类型:
type type1 []struct {
Field1 string
Field2 int
}
type type2 []struct {
Field1 string
Field2 int
}
有没有直接的方法将值从 type1 写入 type2,知道它们具有相同的字段?(除了编写一个将所有字段从源复制到目标的循环之外)
谢谢。
对于您的具体示例,您可以轻松转换它playground
:
t1 := type1{{"A", 1}, {"B", 2}}
t2 := type2(t1)
fmt.Println(t2)