0

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.

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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 (i.e. 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.