简体   繁体   中英

How to store values in struct using a for loop in golang

I want to store values in a struct. I have multiple set of datas and while iterating those set of data, I have to store those sets into struct. I should also have the previous stored data along with the currently stored.

Please find the code i am using

    package main
    import (
      "fmt"
    )

    type saveDetails struct {
      ID string
      Grade string
      Regular string
      OpeningKey string
    }

    func main() {
       tagsList := []saveDetails {}
       results = [{ {1000000001 A Regular JOBOp123}} { {1000000002 B Regular JOBOp234}} { {1000000003 C  Regular JOBOp456}}]

       for _, details := range results {
          tagsList = append(tagsList, saveDetails {ID: details.ID, Grade:details.Grade, Regular:details.Regular, OpeningKey:details.OpeningKey})
       }
       fmt.Println("saveDetails :",tagsList )
     }

Please help me in resolving this issue. I am new to this array and structs in golang. I am not sure whether i could use the append function. It could be very much helpful if i get an working code.

This is a valid and working version of your code:

package main

import (
    "fmt"
)

type saveDetails struct {
    ID         string
    Grade      string
    Regular    string
    OpeningKey string
}

func main() {
    var tagsList []saveDetails
    results := []saveDetails{saveDetails{ID: "1000000001", Grade: "A", Regular: "Regular", OpeningKey: "JOBOp123"}, saveDetails{ID: "1000000001", Grade: "A", Regular: "Regular", OpeningKey: "JOBOp123"}}

    for _, details := range results {
        tagsList = append(tagsList, saveDetails{
            ID: details.ID, Grade: details.Grade, Regular: details.Regular, OpeningKey: details.OpeningKey,
        })
    }
    fmt.Println("saveDetails :", tagsList)
}

https://play.golang.org/p/vVQGPTnph6z

Here is a working solution to your problem. Note that your results array was not correctly declared. Below you can find a working solution where the tagsList is not of type array but uses a slice that is initialized with the make() function. I would suggest using slices, as arrays are value types and therefore always copied when passed around or set under new variables. Slices are just references (ie a pointer) to underlying arrays and have various advantages in terms of performance if the array gets bigger as time passes.

package main

import (
    "fmt"
)

type saveDetails struct {
    ID         string
    Grade      string
    Regular    string
    OpeningKey string
}

func main() {
    tagsList := make([]saveDetails, 0) // 0 is the initial size of the slice
    results := []saveDetails{{"1000000001", "A", "Regular", "JOBOp123"}, {"1000000002", "B", "Regular", "JOBOp234"}, {"1000000003", "C", "Regular", "JOBOp456"}}

    for _, details := range results {
        tagsList = append(tagsList, saveDetails{ID: details.ID, Grade: details.Grade, Regular: details.Regular, OpeningKey: details.OpeningKey})
    }
    fmt.Println("saveDetails :", tagsList)
}

https://play.golang.org/p/Josvx49tNf6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM