This is a creation in Article, where the information may have evolved or changed.
First put the simple code out, and then nonsense.
First benchmark's orders.
go test -bench=Poin -memprofile=pointermem.pprof -memprofilerate=1 -benchmem
go test -bench=Value -memprofile=valuemem.pprof -memprofilerate=1 -benchmem
Then is the result of pprof, first pointer, then value, you can see the pointer report 2allocs/op
package maintype mytype struct {field1 stringfield2 stringfield3 stringfield4 stringfield5 stringfield6 stringfield7 stringfield8 stringfield9 string}func returnValue() mytype {return mytype{field1: "abc",field2: "aaa",field3: "vvv",field4: "ddd",field5: "394834",field6: "sdfbd34534",field7: "asdf34534d90sdfsd",field8: "asdf3424jjiiihiu",field9: "vmxvbxcvb8rewasdfasdf",}}func returnPointer() *mytype {return &mytype{field1: "abc",field2: "aaa",field3: "vvv",field4: "ddd",field5: "394834",field6: "sdfbd34534",field7: "asdf34534d90sdfsd",field8: "asdf3424jjiiihiu",field9: "vmxvbxcvb8rewasdfasdf",}}func main() {}
package mainimport ("fmt""testing")func BenchmarkReturnValue(b *testing.B) {var x mytypefor i := 0; i < b.N; i++ {x = returnValue()fmt.Println(x)}}func BenchmarkReturnPointer(b *testing.B) {var x *mytypefor i := 0; i < b.N; i++ {x = returnPointer()fmt.Println(*x)}}