TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Map[string]interface{} in Go: a user's manual

50 pointsby bitfieldalmost 4 years ago

8 comments

nixpulvisalmost 4 years ago
&gt; What&#x27;s the point of interface{}, then, if it doesn&#x27;t tell us anything about the value? Well, that&#x27;s precisely why it&#x27;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.
评论 #27964077 未加载
评论 #27963055 未加载
评论 #27963174 未加载
akavelalmost 4 years ago
There&#x27;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{ &quot;fooBar&quot;: true, &quot;nested&quot;: loose{ &quot;subfield&quot;: bazArg, }, }) &#x2F;&#x2F; ... } </code></pre> I should really probably write some blog listing various tricks like this I&#x27;m using, but I still can&#x27;t make myself do it...
评论 #27966958 未加载
enricozbalmost 4 years ago
`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&#x27;re making a set, use `map[string]struct{}` instead of `map[string]interface{}` as `struct{}` takes 0 bytes whereas `interface{}` takes 8.
评论 #27964727 未加载
sigmonsaysalmost 4 years ago
depending on what you&#x27;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&#x27;t indicate its &quot;Type&quot; somewhere&#x2F;somehow then you would have to resort to map[string]interface{}
stevebmarkalmost 4 years ago
Why does the syntax specifically use `interface{}` instead of just `interface`? `{}` isn&#x27;t use for any other types
评论 #27964925 未加载
hdhjebebebalmost 4 years ago
I&#x27;m excited for generics to land in Golang so we can have efficient set implementations instead of abusing maps.
评论 #27963646 未加载
andrewmcwattersalmost 4 years ago
`map[string]interface{}` screams disconnected from reality when building web software. What an awful experience.
评论 #27964452 未加载
评论 #27964449 未加载
Blikkentrekkeralmost 4 years ago
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.
评论 #27963693 未加载