假设我有两种类似的类型:
type type1 []struct {
Field1 string
Field2 int
}
type type2 []struct {
Field1 string
Field2 int
}
Run Code Online (Sandbox Code Playgroud)
是否有直接的方法将值从type1写入type2,知道它们具有相同的字段?(除了编写一个循环,将所有字段从源复制到目标)
谢谢.
我有两个struct有相同的成员,我想将一个结构复制到另一个,请参阅下面的伪代码:
type Common struct {
Gender int
From string
To string
}
type Foo struct {
Id string
Name string
Extra Common
}
type Bar struct {
Id string
Name string
Extra Common
}
Run Code Online (Sandbox Code Playgroud)
然后,我有foo结构的Foo,而bar结构的Bar,有什么办法复制bar的foo?
我有这些类型:
type Value interface{}
type NamedValue struct {
Name string
Value Value
}
type ErrorValue struct {
NamedValue
Error error
}
Run Code Online (Sandbox Code Playgroud)
我可以使用v := NamedValue{Name: "fine", Value: 33},但我无法使用e := ErrorValue{Name: "alpha", Value: 123, Error: err}
似乎嵌入语法没问题,但使用它不起作用?