'How to write data to a slice of structs
First, the raw input data:
CPF PRIVATE INCOMPLETO DATA DA ÚLTIMA COMPRA TICKET MÉDIO TICKET DA ÚLTIMA COMPRA LOJA MAIS FREQUÊNTE LOJA DA ÚLTIMA COMPRA
041.091.641-25 0 0 NULL NULL NULL NULL NULL
058.189.421-98 0 0 NULL NULL NULL NULL NULL
769.013.439-49 0 0 NULL NULL NULL NULL NULL
I have this struct:
type row struct {
cpf string
private bool
incompleto bool
ultCompra string
ticketMedio float64
ticketUltimo float64
lojaMaisFreq string
lojaUltCompra string
}
I created a slice of it:
var parsedData []row
And i want to write data in a for range loop. The data comes from a txt and i parsed it.
I did a for range loop with with strings.Field like this:
for i1, v1 := range data {
// ignore the first line because it is the header
if i1 == 0 {
continue
}
lines := strings.Fields(v1)
The data is now a [][]string like this:
[004.350.709-32 0 0 2013-05-27 599,60 599,60 79.379.491/0008-50 79.379.491/0008-50]
[152.110.128-01 0 0 2013-05-27 449,90 449,90 79.379.491/0008-50 79.379.491/0008-50]
[067.737.339-28 0 0 2013-05-27 299,90 299,90 79.379.491/0008-50 79.379.491/0008-50]
So i iterate over it in a for range loop:
for i2, v2 := range data {
fmt.Println(i2, "-", v2)
}
Which gives me this:
0 - 004.350.709-32
1 - 0
2 - 0
3 - 2013-05-27
4 - 599,60
5 - 599,60
6 - 79.379.491/0008-50
7 - 79.379.491/0008-50
After this i created functions to treat all the data to be compatible with each field in the struct.
Now... How do i put everything into the slice of structs? Here is what i'm doing:
// using the index of the first loop
newIndex := i1 - 1 // since it ignores 0 which is the header
switch i2 {
case 0:
parsedData[newIndex].cpf = cleanStrings(v2)
case 1:
parsedData[newIndex].private = strToBool(v2)
case 2:
parsedData[newIndex].incompleto = strToBool(v2)
case 3:
parsedData[newIndex].ultCompra = v2
case 4:
parsedData[newIndex].ticketMedio = strToFloat(v2)
case 5:
parsedData[newIndex].ticketUltimo = strToFloat(v2)
case 6:
parsedData[newIndex].lojaMaisFreq = cleanStrings(v2)
case 7:
parsedData[newIndex].lojaUltCompra = cleanStrings(v2)
}
But this is wrong. It gives me index out of range. Since it is an empty slice I probably have to append to it instead, but i can't wrap my head around how to do this.
I have to create a new row struct for every loop?
Solution 1:[1]
So, i just figured out. I got the number of lines of the txt file with len and made the slice:
parsedData := make([]row, len(data)-1)
for i1, v1 := range data {
if i1 == 0 {
continue
}
lines := strings.Fields(v1)
for i2, v2 := range lines {
newIndex := i1 - 1
switch i2 {
case 0:
parsedData[newIndex].cpf = cleanStrings(v2)
case 1:
parsedData[newIndex].private = strToBool(v2)
case 2:
parsedData[newIndex].incompleto = strToBool(v2)
case 3:
parsedData[newIndex].ultCompra = v2
case 4:
parsedData[newIndex].ticketMedio = strToFloat(v2)
case 5:
parsedData[newIndex].ticketUltimo = strToFloat(v2)
case 6:
parsedData[newIndex].lojaMaisFreq = cleanStrings(v2)
case 7:
parsedData[newIndex].lojaUltCompra = cleanStrings(v2)
}
}
}
return parsedData
And now everything works!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | crdpa |
