I have a data structure in go:
type APIMain struct {
CodeConv string `json:"codeConv"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Details []struct {
IDPrm string `json:"idPrm"`
Keys []struct {
Timestamp time.Time `json:"timestamp"`
Value float64 `json:"value"`
} `json:"keys"`
} `json:"details"`
}
that I need to transform to:
type DataGroupedByTS struct {
CodeConv string `json:"codeConv"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Details []struct {
Timestamp time.Time `json:"timestamp"`
Keys []struct {
IDPrm string `json:"idPrm"`
Value float64 `json:"value"`
} `json:"keys"`
} `json:"details"`
}
I get:
panic: runtime error: index out of range
Here is my method but it is failing on the first line of loop:
func groupByTimestamp(apiMain datacheck.APIMain) DataGroupedByTS {
var dataGrouped DataGroupedByTS
dataGrouped.CodeConv = apiMain.CodeConv
dataGrouped.Start = apiMain.Start
dataGrouped.Start = apiMain.Start
dataGrouped.End = apiMain.End
var iDetail = 0
var iKey = 0
for _, detail := range apiMain.Details {
for _, key := range detail.Keys {
dataGrouped.Details[iDetail].Timestamp = key.Timestamp // <-- failing here
dataGrouped.Details[iDetail].Keys[iKey].IDPrm = detail.IDPrm
dataGrouped.Details[iDetail].Keys[iKey].Value = key.Value
iKey++
}
iDetail++
}
return dataGrouped
}
Basically, data originally comes grouped by IDPrm
, and I need to group it by timestamp.
How should I do that ? Is there any helpers that could help doing it ?