logoalt Hacker News

odo1242today at 6:32 AM2 repliesview on HN

The answer is apparently "you don't":

- Everything in the language is statically allocated or stack-allocated. You have to call a malloc / free function to get heap allocated things

- The language is not memory safe (you can't return slices, pointers, or interface types from a function if the thing was created inside the function, unless you used heap allocation)

- Interfaces (the only variable size struct Go has) are implemented by creating a struct of function pointers. Arrays and maps (the non-struct variable size types) are implemented as stack-only and maps are limited to 1024 keys. You can opt into heap-based arrays / maps in the standard library to bypass this.


Replies

rob74today at 8:08 AM

So much for "Go's safety" in the quote above. Ok, it doesn't explicitly say memory safety, but what other safety could if be referring to?

show 3 replies
brabeltoday at 6:41 AM

It sounds incredibly dangerous in the hands of a usual go programmer who has no idea what the difference between the stack and the heap is.

show 1 reply