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

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

How to write data to a slice of structs

问题

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

for i1, v1 := range data {
    // ignore the first line because it is the header
    if i1 == 0 {
        continue
    }

    lines := strings.Fields(v1)

    // create a new row struct
    newRow := row{}

    for i2, v2 := range lines {
        switch i2 {
        case 0:
            newRow.cpf = cleanStrings(v2)
        case 1:
            newRow.private = strToBool(v2)
        case 2:
            newRow.incompleto = strToBool(v2)
        case 3:
            newRow.ultCompra = v2
        case 4:
            newRow.ticketMedio = strToFloat(v2)
        case 5:
            newRow.ticketUltimo = strToFloat(v2)
        case 6:
            newRow.lojaMaisFreq = cleanStrings(v2)
        case 7:
            newRow.lojaUltCompra = cleanStrings(v2)
        }
    }

    // append the new row to parsedData
    parsedData = append(parsedData, newRow)
}

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

英文:

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?

答案1

得分: 1

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

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

现在一切都正常了!

英文:

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!

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:

确定