Take this example:
type Foo struct {
num int
}
// func NewFoo() *Foo { // returning a pointer
// return &Foo{33}
// }
func NewFoo() Foo { // NOT returning a pointer
return Foo{33}
}
func main() {
fff := NewFoo()
fmt.Printf("%d\n", fff.num)
}
If NewFoo returns a pointer, I understand the object is kept in the heap, and fff receives the pointer to the very same heap-allocated object.
Now, with the second implementation of NewFoo, which won't return a pointer, will Go return a copy of the stack-allocated struct, or is there something like C++'s RVO taking place?
You can check it by following these steps:
Write your program in a file say
main.golike this: https://play.golang.org/p/iwxai0EHa40Execute the command
go build -gcflags=-m main.goSee the output to really know if it is being copied/inlined. In my case the output I get is:
This procedure you can follow in any Go program to check if there is any unnecessary heap allocation occurring in your program which you can avoid.
Here is a short article which goes through various ways you can make your program more efficient.