Logo

Programming-Idioms

History of Idiom 209 > diff from v10 to v11

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

Version 10

2020-03-20, 17:27:34

Version 11

2020-03-20, 17:33:26

Idiom #209 Type with automatic deep deallocation

Declare a type t which contains a string s and an integer array n with variable size, and allocate a variable v of type t. Allocate v.s and v.n and set them to the values "Hello, world!" for s and [1,4,9,16,25], respectively. Deallocate v, automatically deallocating v.s and v.n (no memory leaks).

Idiom #209 Type with automatic deep deallocation

Declare a type t which contains a string s and an integer array n with variable size, and allocate a variable v of type t. Allocate v.s and v.n and set them to the values "Hello, world!" for s and [1,4,9,16,25], respectively. Deallocate v, automatically deallocating v.s and v.n (no memory leaks).

Code
type t struct {
	s string
	n []int
}

v := t{
	s: "Hello, world!",
	n: []int{1, 4, 9, 16, 25},
}
Comments bubble
After v goes out of scope, v and all its fields will be garbage-collected, recursively
Demo URL
https://play.golang.org/p/xsK4Qrf0n2Y