3 ms·
Not 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 wha
by zhengyi13 2y ago
Not 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".