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 關注
- 120 瀏覽
添加回答
舉報