Logo

Programming-Idioms

History of Idiom 207 > diff from v7 to v8

Edit summary for version 8 by programming-idioms.org:
New Go implementation by user [programming-idioms.org]

Version 7

2020-03-17, 13:34:10

Version 8

2020-03-20, 17:23:38

Idiom #207 allocate a list that is automatically deallocated

Allocate a list / array a containing n elements (n assumed to be too large for a stack) that is automatically deallocated when the program exits the scope it is declared in.

Idiom #207 allocate a list that is automatically deallocated

Allocate a list / array a containing n elements (n assumed to be too large for a stack) that is automatically deallocated when the program exits the scope it is declared in.

Code
a := make([]T, n)
Comments bubble
Elements have type T.
a is garbage-collected after the program exits its scope, unless we let it "escape" by taking its reference.
The runtime decides if a lives in the stack on in the heap.
Demo URL
https://play.golang.org/p/ifFdj8Bhl2j