Create a Map of Array of Structs in golang?

user8800020 picture user8800020 · Jun 14, 2018 · Viewed 9.7k times · Source

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}]]

Answer

Zak picture Zak · Jun 14, 2018

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