A pointer is a Go value that represents the memory address of another value. Use the address operator & to obtain an address and the indirection operator * to access the value stored there. section 2.4.1
Pointers become useful when an operation must work with an existing variable rather than a copy:
- Pass a pointer to a function when the function must alter the caller's variable; Go function arguments are otherwise passed by copy. section 2.4.1
- Give flag.StringVar the address of a string so command-line parsing can fill that variable. section 2.4.1
- Remember that Go provides no pointer arithmetic. section 2.4.1
Binding a command-line flag
Declare a destination, then pass its address: var lang string; flag.StringVar(&lang, "lang", "en", "The required language"). After flag.Parse(), lang contains the supplied value or its default.
Pointer choices also affect design. The shared http.DefaultClient is itself a pointer, so changing it changes the client used across the program. A private http.Client can instead keep timeout behavior local to one component. Timeout errors may be represented by *url.Error, which can be inspected through errors.As. section 6.6.2
















