Create a Map of Array of Structs in golang?

12,070

You can define a custom type, backed by a map, and then define a custom unmarshaller on that type.

Here is a runnable example in the go playground

// the value in the map that you are unmarshalling to
type TCPValue struct {
    TcID string
    Time string
}

// the map type you are unmarshalling to
type TCPSet map[string][]TCPValue

// custom unmarshalling method that implements json.Unmarshaller interface
func (t *TCPSet) UnmarshalJSON(b []byte) error {
    // Create a local struct that mirrors the data being unmarshalled
    type tcEntry struct {
        TcID string `json:"tcID"`
        TCP string `json:"tcp"`
        Time string `json:"time"`
    }

    var entries []tcEntry

    // unmarshal the data into the slice
    if err := json.Unmarshal(b, &entries); err != nil {
        return err
    }

    tmp := make(TCPSet)

    // loop over the slice and create the map of entries
    for _, ent := range entries {
        tmp[ent.TCP] = append(tmp[ent.TCP], TCPValue{TcID: ent.TcID, Time: ent.Time})
    }

    // assign the tmp map to the type
    *t = tmp
    return nil
} 

You'll be able to access the elements like a regular map:

elem := tcpSet["/home/1.py"]

Edited based on comment from OP to be map[string][]TCPValue

Share:
12,070
user8800020
Author by

user8800020

Updated on June 04, 2022

Comments

  • user8800020
    user8800020 about 2 years

    I had a Json of format

    {
        ...,
        "tclist":[{
            "tcID":"TC1",
            "tcp":"/home/1.py",
            "time":"20:00:40"
        }, {
            "tcID":"TC2",
            "tcp":"/home/1.py",
            "time":"048:50:06"
        }],
        ...
    }
    

    I want to create a Map that takes a tcp as key and add the tcID and time to it as a entry in a set.

    I Want

    [["/home/1.py"][{tcID,Time},{tcID,Time}],[["/home/2.py"][{tcID,Time},{tcID,Time}]]
    
    • Thomas
      Thomas about 6 years
      Parse it how it is, transform it to how you want. What are you looking for here?
    • user8800020
      user8800020 about 6 years
      i wanted it to work like a hashset in java but it raises errors can you tell me how to do it
    • mbuechmann
      mbuechmann about 6 years
      What raises an error? Could you show us your code?
  • user8800020
    user8800020 about 6 years
    What you produced is map[/home/1.py:{TcID:TC3 Time:20:20:20} /home/2.py:{TcID:TC2 Time:048:50:06}] I wanted is map[/home/1.py:[{TcID:TC3 Time:20:20:20},{TcId:TC2 Time:20:20:12}] /home/2.py:[{TcID:TC2 Time:048:50:06}]]