Hi,
I would like to be able to store multiple different structs in a JSON file that I can read in to perform specific operations. One of the structs has multiple fields, e.g.:
type copyFile struct {
Src string `json:"src"`
Dst string `json:"dst"`
Sudo bool `json:"sudo"`
OverWrite bool `json:"overwrite"`
}
And another struct that is just user commands, e.g.:
type userCommand struct {
Cmd string `json:"cmd"`
}
If I put them both in an array and json.Marshal them I get something along the lines of:
[{“cmd”:“echo user command”},{“src”:"/dir/src",“dst”:"/dir/dst",“sudo”:true,“overwrite”:false}]
How would I Unmarshal that?
For now I’ve changed the copyFile struct to:
type copyFile struct {
Cmd string `json:"copy"`
}
I then catenate the src, dst, sudo and overwrite with a separator which then allows me to Unmarshal the whole thing with:
x, _ := json.Marshal(commandsArray)
var data []map[string]string
err := json.Unmarshall(x,&data)
Which works and I can just strings.Split() the copyFile Cmd. I just didn’t know if there’s another way to do this that would maintain the structs.
Does anyone have any suggestions? I’m new to Go, so I’m tapping around in the dark at the moment.
Thanks,
Rob