> What's the point of interface{}, then, if it doesn't tell us anything about the value? Well, that's precisely why it's useful: it can refer to anything!<p>An empty interface should be effectively useless. What could it possibly do!? This pattern is much more like a dynamic container with a type tag.
There's a nice trick you can use to make your map[string]interface{} code a bit easier to write and arguably also to read (less {}s), useful especially when you need e.g. to serialize some deeply nested JSON, for one-off request:<p><pre><code> func doSomeRequest(...) {
type loose map[string]interface{}
payload, err := json.Marshal(loose{
"fooBar": true,
"nested": loose{
"subfield": bazArg,
},
})
// ...
}
</code></pre>
I should really probably write some blog listing various tricks like this I'm using, but I still can't make myself do it...
`map[string]interface{}` is just a map with string keys and `any` as a value, and is commonly used as the type for a JSON object. Unless you have a completely specified type for your JSON object (at that point might as well use a `struct` with all of the fields defined), the best you _could_ do would be something like a recursive type `type JSON = bool | string | []JSON | map[string]JSON` in some hypothetical version of Golang with recursive types and unions.<p>Also, if you're making a set, use `map[string]struct{}` instead of `map[string]interface{}` as `struct{}` takes 0 bytes whereas `interface{}` takes 8.
depending on what you're trying to do a map[string]interface{} is not really want you want. Most likely you want map[string]json.RawMessage if the key can indicate what the next step is in umarshalling.<p>if the serialized structure doesn't indicate its "Type" somewhere/somehow then you would have to resort to map[string]interface{}
This article almost seems like a falseflag, in how it cheerfully provides a benediction for an ugly hack to deal with controversial inadequacies of the <i>Go</i> type system.