我有两个 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
}
Then I have foo
of struct Foo
, and bar
of struct Bar
, Is there any way to copy bar
来自 foo
?
原文由 jsvisa 发布,翻译遵循 CC BY-SA 4.0 许可协议
使用 转换 来更改类型。以下代码使用转换将类型 --- 的值复制到类型
Foo
Bar
值:游乐场示例
仅当基础类型除结构标记外都相同时, 转换 才有效。