0

I have a struct as below:

type TourData struct {
    ArtistID   int    //artist ID
    RelationID string //key for relations
    City       string
    Country    string
    TourDates  []string
}

type MyRelation struct {
    ID             int                 `json:"id"`
    DatesLocations map[string][]string `json:"datesLocations"`
}

which contains this data from a csv file:

1,nagoya-japan,Nagoya,Japan,
1,penrose-new_zealand,Penrose,New_Zealand,
1,dunedin-new_zealand,Dunedin,New_Zealand,
2,playa_del_carmen-mexico,Playa Del Carmen,Mexico,
2,papeete-french_polynesia,Papeete,French_Polynesia,

MyRelations is populated from an API which contains:

"index": [
        {
            "id": 1,
            "datesLocations": {
                "dunedin-new_zealand": [
                    "10-02-2020"
                ],
                "nagoya-japan": [
                    "30-01-2019"
                ],
                "penrose-new_zealand": [
                    "07-02-2020"
                ]
            }
        },
        {
            "id": 2,
            "datesLocations": {
                "papeete-french_polynesia": [
                    "16-11-2019"
                ],
                "playa_del_carmen-mexico": [
                    "05-12-2019",
                    "06-12-2019",
                    "07-12-2019",
                    "08-12-2019",
                    "09-12-2019"
                ]
            }
        }

The dates come from another struct. The code I have used to populate this struct is as below:

var oneRecord TourData
    var allRecords []TourData

for _, each := range csvData {
    oneRecord.ArtistID, _ = strconv.Atoi(each[0])
    oneRecord.RelationID = each[1]
    oneRecord.City = each[2]
    oneRecord.Country = each[3]
    oneRecord.TourDates = Relations.Index[oneRecord.ArtistID-1].DatesLocations[each[1]]
    allRecords = append(allRecords, oneRecord)
}
    
jsondata, err := json.Marshal(allRecords) // convert to JSON
json.Unmarshal(jsondata, &TourThings)

I need to group all the 1s together then the 2s etc. I thought to create another struct, and populate from this one but not having much luck - any ideas?

To clarify I would want say TourData.City to equal:

[Nagoya,Penrose,Dunedin]
[Playa Del Carmen, Papeete]

At the moment if I was to print TourData[0].City I would get Nagoya.

I have tried creating another struct to be populated from the TourData struct with the following fields:

type TourDataArrays struct {
    ArtistID  int
    City      []string
    Country   []string
    TourDates [][]string
}

and then populate the struct using the code below:

var tourRecord TourDataArrays
    var tourRecords []TourDataArrays
    for i := 0; i < len(Relations.Index); i++ {
        for j := 0; j < len(allRecords); j++ {
            if allRecords[i].ArtistID == i+1 {
                tourRecord.City = append(tourRecord.City, allRecords[j].City)
            }
        }
        tourRecords = append(tourRecords, tourRecord)
    }

However this is adding all the cities to one array i.e

[Nagoya, Penrose, Dunedin, Playa Del Carmen, Papeete].

3
  • 1
    What do you mean by "group all the 1s together"; do you just want the slice sorted (ordered by 1 then 2, then 3 etc)?. Where the data comes from does not appear relevant; perhaps give us a sample input/expected output in JSON. Commented Mar 2, 2022 at 19:24
  • I have edited my question - is that ok? Commented Mar 2, 2022 at 19:57
  • Sorry - your change introduces another undefined type (TourDataArrays) along with those already there (Relations, oneRecord etc). Please aim for a minimal, reproducible example; something like this (which may already answer your question). Commented Mar 2, 2022 at 20:33

1 Answer 1

1

If I understand your requirements correctly you needed to declare city as a string array as well. (And Country to go with it).

Check out this solution : https://go.dev/play/p/osgkbfWV3c5

Note I have not deduped country and derived city and country from one field in the Json.

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

5 Comments

Thank you that is so much more concise than what I was doing. I feel a bit like give an inch take a ruler here but would it then be possible to sort the array by country as the list is currently in a random order?
Just to give some clarification as to the end goal I want to create a html table which will list country, city and dates ordered by country.
And another thing I'm trying to unmarshal from an API but keep getting this error panic: json: cannot unmarshal object into Go value of type []main.MyRelation any ideas?
Have you tried sorting it? The sort (pkg.go.dev/[email protected]) package provides functions for that. Reg your unmarshal error, cannot really say anything unless the code is seen. please see stackoverflow.com/help/minimal-reproducible-example.
I think the question would be overly long if I posted that code in too! I've managed to do something which is probably hodge podge but I can access the elements in the struct now just need to figure out a way of creating a table from it. Thank you again for all your time :)

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.