vlang.org is a fun new take on a go-like language. managing data and references to it is still challenging and requires a good bit of intuitive knowledge. mostly coming from being able to answer "if I were to think about how this is implemented C, how would it work?" and applying that to what is mostly likely going on under the covers in v.
This is my attempt at a cheatsheet for statements in v and their consequences as far as copy by value, copy by reference, and allocations on the stack or the heap. This was written with v weekly 2021.6 on linux/x86. the 0x7... upper addresses are considered to be stack, and the lower addresses are considered to be heap.
A Thing is a struct with one field, its name.
struct Thing { name: string }
Create a new Thing
thing1 := Thing{
name: 'thing1'
}
println('thing1 ${typeof(thing1).name} ${ptr_str(thing1)}')
Thing 7ffd4dc5da88
Create a new Thing and cue the compiler for heap allocation
thing_heap := &Thing{
name: 'thing1'
}
println('thing_heap ${typeof(thing_heap).name} ${ptr_str(thing_heap)}')
thing_heap 879c10
Create an array of Thing
&Thing{} -> heap
[]Thing -> heap
[10]Thing -> stack
maps -> heap
for thing in things
for mut thing in things