4 ms·
Hold on, did you just say Go doesn't have a Dictionary data type? I'm a Javascript, Lua, Python, and C# guy and Dict is my whole world.
by jay_kyburz 2y ago
Hold on, did you just say Go doesn't have a Dictionary data type?
I'm a Javascript, Lua, Python, and C# guy and Dict is my whole world.
- bitwize 2y agoGo has had a dict-like data type from the jump; they're called "maps" in Go. Some of early Go's design decisions were kinda stupid, but they didn't screw that one up.
- AmpsterMan 2y agoIt does. https://go.dev/blog/maps https://go.dev/blog/maps What the poster was alluding to is that you usually prefer to deseialize to a struct rather than a record/dict/map
- zhengyi13 2y agoNot a programmer, so this is every programmer's chance to hammer me on correctness. No, Go doesn't have a type named Dict, or Hash (my Perl is leaking), or whatever. It does have a map type[1], where you can define your keys as one type, and your values of another type, and that pretty closely approximates Dicts in other languages, I think. [1]: https://go.dev/blog/maps https://go.dev/blog/maps
- duskwuff 2y agoAnd, if you hate strong typing, there's always map[string]any.
- dwattttt 2y agoReally, the mismatch is at the JSON side; arbitrary JSON is the opposite of strongly typed. How a language lets you handle the (easily fallible) process of "JSON -> arbitrarily typed -> the actual type you wanted" is what matters.
- throwaway2037 2y ago> arbitrary JSON is the opposite of strongly typed On the surface, I agree. In practice, many big enterprise systems use highly dynamic JSON payloads where new fields are added and changed all the time.
- mjevans 2y agoI think I'd want to try to decode into map[string]interface{} (offhand), since string keys can be coerced to that in any event (they're strings in the stream, quoted or otherwise), and a key can hold any valid json scalar, array, or object (another json sub-string).
- simiones 2y agoThat of course works, but the problem is then using this. Take a simple JSON like `{"list": [{"field": 8}]}`. To retrieve that value of 8, your Go code will look sort of like this: var v map[string]any json.Unmarshal(myjson, &v) lst := v["list"].([]any) firstItem := lst[0].(map[string]any) field := firstItem["field"].(float64) And this is without any error checking (this code will panic if myjson isn't a json byte array, if the keys and types don't match, or if the list is empty). If you want to add error checking to avoid panics, it gets much longer [0]. Here is the equivalent Python with full error checking: try : v = json.loads(myjson) field = v["list"][0]["list"] except Exception as e: print(f"Failed parsing json: {e}") [0] https://go.dev/play/p/xkspENB80JZ https://go.dev/play/p/xkspENB80JZ
- tialaramex 2y agoSo, these types (and many more) are hash tables. https://en.wikipedia.org/wiki/Hash_table https://en.wikipedia.org/wiki/Hash_table They're a very common and useful idea from Computer Science so you will find them in pretty much any modern language, there are a lot of variations on this idea, but the core idea recurs everywhere.
- simiones 2y agoI have a quibble here. A hash table, the basic CS data structure, is not a two-dimensional data structure like a map, it is a one-dimensional data structure like a list. You can use a hash table to implement a Map/Dictionary, and essentially everyone does that. Sets are also often implemented using a hash table. The basic operations of a hash table are adding a new item to the hash table, and checking if an item is present (potentially removing it as well). A hash table doesn't naturally have a `V get(key K)` function, it only naturally has a `bool isPresent(K item)` function. This is all relevant because not all maps use hash tables (e.g. Java has TreeMap as well, which uses a red-black tree to store the keys). And there are uses of hash tables besides maps, such as a HashSet. Edit: the term "table" in the name refers to the internal two-dimensional structure: it stores a hash, and for each hash, a (list of) key(s) corresponding to that hash. Storing a value alongside the key is a third "dimension".
- deleted 2y ago[deleted]
- absoflutely 2y agoGo has maps, json parsing and http built in. I'm not exactly sure what this person is referring to. Perhaps they are mostly interviewing beginners?
- marcus_holmes 2y agoGo maps have a defined type (like map[string]string), so you can only put values of that type in them. A JSON object with (e.g) numbers in it will fail if you try and parse that into a map of strings. As others have said, the issue with Go parsing JSON is that Go doesn't handle unstructured data at all well, and most other languages consider JSON to be unstructured data. Go expects the JSON to be strongly typed and rigidly defined, mirroring a struct in the Go code that it can use as a receiver for the values. There are techniques for handling this, but they're not obvious and usually learned by painful experience. This is not all Go's fault - there are too many endpoints out there that return wildly variable JSON depending on context.
- claytongulick 2y agoI feel like good JSON handling is sort of table stakes for any language for me these days. The pain of dealing with JSON in Go is one of the primary reasons I stick mostly with nodejs for my api servers.
- lelanthran 2y ago> The pain of dealing with JSON in Go is one of the primary reasons I stick mostly with nodejs for my api servers. Unless you're dealing with JSON input that has missing fields, or unexpected fields, there is no pain. Go can natively turn a JSON payload into a struct as long as the payload's fields recursively match the struct's fields! If, in any language, you're consuming or generating JSON that doesn't match a specific predetermined structure, you're yolo'ing it and all bets are off. Go makes this particualr footgun hard to do, while JS, Python, etc makes it the default. Default footguns are a bad idea, not a good idea.
- 2y ago
- mseepgood 2y agoIt does (it's called a map) and Go does have generics, the previous poster clearly doesn't know what they're talking about.