如何将数据写入结构体切片中

huangapple go评论138阅读模式
英文:

How to write data to a slice of structs

问题

首先,你需要在循环中创建一个新的row结构体,并将其添加到parsedData切片中。你可以使用append函数来实现这一点。以下是你可以尝试的修改:

  1. for i1, v1 := range data {
  2. // ignore the first line because it is the header
  3. if i1 == 0 {
  4. continue
  5. }
  6. lines := strings.Fields(v1)
  7. // create a new row struct
  8. newRow := row{}
  9. for i2, v2 := range lines {
  10. switch i2 {
  11. case 0:
  12. newRow.cpf = cleanStrings(v2)
  13. case 1:
  14. newRow.private = strToBool(v2)
  15. case 2:
  16. newRow.incompleto = strToBool(v2)
  17. case 3:
  18. newRow.ultCompra = v2
  19. case 4:
  20. newRow.ticketMedio = strToFloat(v2)
  21. case 5:
  22. newRow.ticketUltimo = strToFloat(v2)
  23. case 6:
  24. newRow.lojaMaisFreq = cleanStrings(v2)
  25. case 7:
  26. newRow.lojaUltCompra = cleanStrings(v2)
  27. }
  28. }
  29. // append the new row to parsedData
  30. parsedData = append(parsedData, newRow)
  31. }

这样,每次循环时都会创建一个新的row结构体,并将其添加到parsedData切片中。这样你就可以将所有数据放入parsedData中的结构体切片了。

英文:

First, the raw input data:

  1. CPF PRIVATE INCOMPLETO DATA DA ÚLTIMA COMPRA TICKET MÉDIO TICKET DA ÚLTIMA COMPRA LOJA MAIS FREQUÊNTE LOJA DA ÚLTIMA COMPRA
  2. 041.091.641-25 0 0 NULL NULL NULL NULL NULL
  3. 058.189.421-98 0 0 NULL NULL NULL NULL NULL
  4. 769.013.439-49 0 0 NULL NULL NULL NULL NULL

I have this struct:

  1. type row struct {
  2. cpf string
  3. private bool
  4. incompleto bool
  5. ultCompra string
  6. ticketMedio float64
  7. ticketUltimo float64
  8. lojaMaisFreq string
  9. lojaUltCompra string
  10. }

I created a slice of it:

  1. 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:

  1. for i1, v1 := range data {
  2. // ignore the first line because it is the header
  3. if i1 == 0 {
  4. continue
  5. }
  6. lines := strings.Fields(v1)

The data is now a [][]string like this:

  1. [004.350.709-32 0 0 2013-05-27 599,60 599,60 79.379.491/0008-50 79.379.491/0008-50]
  2. [152.110.128-01 0 0 2013-05-27 449,90 449,90 79.379.491/0008-50 79.379.491/0008-50]
  3. [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:

  1. for i2, v2 := range data {
  2. fmt.Println(i2, "-", v2)
  3. }

Which gives me this:

  1. 0 - 004.350.709-32
  2. 1 - 0
  3. 2 - 0
  4. 3 - 2013-05-27
  5. 4 - 599,60
  6. 5 - 599,60
  7. 6 - 79.379.491/0008-50
  8. 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:

  1. // using the index of the first loop
  2. newIndex := i1 - 1 // since it ignores 0 which is the header
  3. switch i2 {
  4. case 0:
  5. parsedData[newIndex].cpf = cleanStrings(v2)
  6. case 1:
  7. parsedData[newIndex].private = strToBool(v2)
  8. case 2:
  9. parsedData[newIndex].incompleto = strToBool(v2)
  10. case 3:
  11. parsedData[newIndex].ultCompra = v2
  12. case 4:
  13. parsedData[newIndex].ticketMedio = strToFloat(v2)
  14. case 5:
  15. parsedData[newIndex].ticketUltimo = strToFloat(v2)
  16. case 6:
  17. parsedData[newIndex].lojaMaisFreq = cleanStrings(v2)
  18. case 7:
  19. parsedData[newIndex].lojaUltCompra = cleanStrings(v2)
  20. }

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?

答案1

得分: 1

所以,我刚刚弄清楚了。我用len函数得到了txt文件的行数,并进行了切片:

  1. parsedData := make([]row, len(data)-1)
  2. for i1, v1 := range data {
  3. if i1 == 0 {
  4. continue
  5. }
  6. lines := strings.Fields(v1)
  7. for i2, v2 := range lines {
  8. newIndex := i1 - 1
  9. switch i2 {
  10. case 0:
  11. parsedData[newIndex].cpf = cleanStrings(v2)
  12. case 1:
  13. parsedData[newIndex].private = strToBool(v2)
  14. case 2:
  15. parsedData[newIndex].incompleto = strToBool(v2)
  16. case 3:
  17. parsedData[newIndex].ultCompra = v2
  18. case 4:
  19. parsedData[newIndex].ticketMedio = strToFloat(v2)
  20. case 5:
  21. parsedData[newIndex].ticketUltimo = strToFloat(v2)
  22. case 6:
  23. parsedData[newIndex].lojaMaisFreq = cleanStrings(v2)
  24. case 7:
  25. parsedData[newIndex].lojaUltCompra = cleanStrings(v2)
  26. }
  27. }
  28. }
  29. return parsedData

现在一切都正常了!

英文:

So, i just figured out. I got the number of lines of the txt file with len and made the slice:

  1. parsedData := make([]row, len(data)-1)
  2. for i1, v1 := range data {
  3. if i1 == 0 {
  4. continue
  5. }
  6. lines := strings.Fields(v1)
  7. for i2, v2 := range lines {
  8. newIndex := i1 - 1
  9. switch i2 {
  10. case 0:
  11. parsedData[newIndex].cpf = cleanStrings(v2)
  12. case 1:
  13. parsedData[newIndex].private = strToBool(v2)
  14. case 2:
  15. parsedData[newIndex].incompleto = strToBool(v2)
  16. case 3:
  17. parsedData[newIndex].ultCompra = v2
  18. case 4:
  19. parsedData[newIndex].ticketMedio = strToFloat(v2)
  20. case 5:
  21. parsedData[newIndex].ticketUltimo = strToFloat(v2)
  22. case 6:
  23. parsedData[newIndex].lojaMaisFreq = cleanStrings(v2)
  24. case 7:
  25. parsedData[newIndex].lojaUltCompra = cleanStrings(v2)
  26. }
  27. }
  28. }
  29. return parsedData

And now everything works!

huangapple
  • 本文由 发表于 2022年3月19日 22:36:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/71539055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定