I'm learning golang. I am trying to store a little bit of data, which I will then send to file.
What would be the golang equivalent of this python block? (a list of dicts)
friends = [
{
"name":"James",
"age":44,
},
{
"name":"Jane",
"age":47,
},
]
Would it be better to use a slice of maps or a struct?
I want to be able to filter the data (for example all friends over 45) and sort the data (lets imagine I've got 12+ records). then print it to the screen.
Many use cases where you use a dict in python, you want a struct for in Go, to get static typing. In your case this looks like a slice of structs to me:
type Friend struct{
Name string `json:"name"`
Age int `json:"age"`
}
and then you can serialize/deserialize to []*Person
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With