我有:
type MyType string type Source struct { Value *string } type Target struct { Value *MyType }
这些是由代码生成器根据 JSON 架构创建的生成类型。
如何将 Source.Value
分配给 Target.Value
?
这是正确的吗:
var target Target var source Source target.Value = new(MyType) *target.Value = MyType(*source.Value)
或者有更好的方法吗?
是的。
(但也许我错过了你问题的要点,因为尝试这个问题比在这里问这个问题花费的时间要少一个数量级。)