News for hackers. Stuff that matters.
Re:Map[string]interface{} in Go: a user's manual by jhoechtl on Monday July 26, 2021 @03:21PM (#27964077)
This article would be great to be revised once parametric types (AKA "generics") become an official part of the language.

https://go.googlesource.com/proposal/+/master/design/15292/2...

https://go2goplay.golang.org/

Re:Map[string]interface{} in Go: a user's manual by kjksf on Monday July 26, 2021 @03:40PM (#27964364)
map in Go is already generic. If you need map[string]int or map[int]float, you can already do it.

Generics do not obviate the need for interface{} or the described use case of map[string]interface{}

interface{} is like Java or C# object. It packages a type and a value in a single value that can be queried at runtime to extract the underlying value in a type safe matter.

When people use map[string]interface{] it's because they don't know the type of value at compile time.

Re:Map[string]interface{} in Go: a user's manual by cle on Monday July 26, 2021 @04:09PM (#27964737)
I'm not sure why you're being downvoted, the only alternatives I've seen are to use methods or enums, which constrain the types to those supported by JSON, but ultimately you still have to switch on the type at runtime, the tradeoff being between closed enums vs. open interfaces, or the runtime tradeoffs of methods. Rust's serde_json uses enums for this, but the maps are generic in the same way that they already are in Go.
Re:Map[string]interface{} in Go: a user's manual by ibraheemdev on Monday July 26, 2021 @04:24PM (#27964967)
> the maps are generic in the same way that they already are in Go.

Go maps are actually not generic in the same way they are in other languages, there is just a single map struct with type descriptors used to do some compiler magic: https://dave.cheney.net/2018/05/29/how-the-go-runtime-implem...

Re:Map[string]interface{} in Go: a user's manual by cle on Monday July 26, 2021 @05:25PM (#27965698)
Yes I'm aware that there are differences, but it's not clear to me how that's relevant.
Re:Map[string]interface{} in Go: a user's manual by ibraheemdev on Monday July 26, 2021 @05:50PM (#27965929)
Well you said "maps are generic in the same way that they already are in Go", which I took to mean that Go already has monomorphized generics under the hood, they just aren't exposed publicly.
Re:Map[string]interface{} in Go: a user's manual by cle on Wednesday July 28, 2021 @10:10AM (#27983947)
I'm specifically talking about the subset of ways in which they are similar and also relevant to this scenario of handling arbitrary structured data like JSON. There are of course ways in which they are not similar, but I'm not sure how those are relevant to this scenario being discussed....could you elaborate on that?
Re:Map[string]interface{} in Go: a user's manual by cratermoon on Monday July 26, 2021 @04:52PM (#27965305)
Yeah, no. That's not what generics mean. A map of any single type with any type for the key is different from a map using generic types.

For generics what you want is not interface{}, which says nothing, but a type T that has meaningful constraints.