August 2019
Beginner to intermediate
798 pages
17h 2m
English
As illustrated in pointerStruct.go from Chapter 4, The Uses of Composite Types, it is considered good practice to create new structure variables using a separate function and return a pointer to them from that function. So, the scenario of functions returning pointers is very common. Generally speaking, such a function simplifies the structure of a program and allows the developer to concentrate on more important things instead of copying the same Go code all the time. This section will use a much simpler example, as found in the Go code of returnPtr.go.
The first part of returnPtr.go contains the following Go code:
package main
import (
"fmt"
)
func returnPtr(x int) *int {
y := x * x
return &y
}
Apart from ...
Read now
Unlock full access