1 回答
TA贡献1828条经验 获得超6个赞
您可以将 map 函数的参数更新为 struct2,并从函数遍历 struct3 的数组字段,并将每个字段发送到函数。maintoMap
func main() {
type Struct3 struct {
Failed []Struct2 `json:"failed"` // a list of transactions
Success []Struct2 `json:"success"`
}
s3 := &Struct3{
Failed: make([]Struct2, 0),
Success: make([]Struct2, 0),
}
for i := range s3.Success {
// do something with the result value
_ = toMap(s3.Success[i])
}
for i := range s3.Failed {
// do something with the result value
_ = toMap(s3.Failed[i])
}
}
func mapTo(st2 Struct2) Struct1 {
st1 := Transaction{}
// the mapping between A => C is quite lengthy
st1.someField = st2.anotherField
return st1 // now mapped
}
- 1 回答
- 0 关注
- 97 浏览
添加回答
举报