Learning Go. Does anyone actually like their type syntax? I mean, come on, `map[string]int`?

Reply to this note

Please Login to reply.

Discussion

?cid=2154d3d7e2pa2tbnk15lixpqcgxxy3rlwcb8z48uwnjfsbj0&ep=v1_gifs_search&rid=giphy.gif&ct=g

Or even better, `map[string][]map[string]int`. In typescript that would be `Record[]>`

No. Rust is better

rust is just C++ with a better build system than Cmake and an extra, super complicated syntax for memory management

isn't it?

There is not syntax for memory management. Memory is managed by the types themselves, in "unsafe" code, that is allowed to do pointer stuff. A String handles the heap allocations and frees in the drop method/destructor. Memory is no different to other resources, a File closes the file in it's drop method, a mutex guard unlocks in it's drop method, etc.

A term I've heard, that stuck with me, was it's type system is "pragmatic", because of the fact, that there is no syntax for memory management. If you need to manage memory, then it is a low level task, just the same as reading some CPU registers.

How could a type system not just infer what the type is…

Because you still need to name the type sometimes.

I'm using the long form for arrays in TS, because of same argument you're making. `Array` instead of `number[]`

Or even more apparent for complex types like your example: `Record>>` instead of `Record[]>`

yes, i like it very much

you should be very careful messing with maps, you may not need maps

maps are not concurrent safe and iterate randomly

i built a key/value collection type, took me an afternoon, that does much of what a map does, but preserves field ordering

also, the reason why go's maps are so basic is because go doesn't force you to use any high level abstractions more complex than this map for your data types

if you need large tables, better to use a database like badger or leveldb

if you need proper ordering, you need to write code that unpacks the map into a slice before rendering it, or you can use a heap or other similar ordered complex data type, container/heap is where the priority queue heap implementation lives

main thing you are going to learn about go is that it's not easy to work with dynamic typed wire encodings like JSON this is why i've spent a lot of time on making sure my in-development relay codebase has some clean and precise implementations that work with Nostr's json types