I have a struct like this:
type Headers struct {
header string
valueFromCalculation string
value float64
}
I need have three slices with the values for each of these:
var headerLabels []string
var values []float64
var valueFromCalculation []string
[January February March April May June July August September TOTAL]
[175 167 148 142 125 114 130 120 30 1151]
[15% 15% 13% 12% 11% 10% 11% 10% 3%]
Now I want to create a new slice of Headers by combining these. There is one issue, that I believe i'm solving - the length of the valueFromCalcuation is 1 less than the rest of the slices
To create the new slice I want to do this:
sliceOfHeaders := []*Headers{}
for i := 0; i <= len(headerLabels); i++ {
headerEntry := new(Headers)
headerEntry.header = headerLabels[i]
headerEntry.value = values[i]
if i == len(headerLabels) {
headerEntry.valueFromCalculation = ""
} else {
headerEntry.valueFromCalculation = valueFromCalculation[i]
}
sliceOfHeaders = append(sliceOfHeaders, headerEntry)
}
It is throwing the below error:
"panic: runtime error: index out of range"
How can this be?
I'm accounting for the issue of the index with the valueFromCalculation length being one less than the other struct properties
Here you can see output from before I want to start my loop:
header --> [January February March April May June July August September TOTAL]
value --> [175 167 148 142 125 114 130 120 30 1151]
valueFromCalculation --> [15% 15% 13% 12% 11% 10% 11% 10% 3%]
header length --> 10
value length --> 10
valueFromCalculation length --> 9
Please can someone help me here? I can't see what i'm doing wrong
I'm accounting for the length of the 3rd property being 1 less than the rest