liveBook · Manning

Reference

As explained in Learn Go with Pocket-Sized Projects

A pointer is a Go value that represents the memory address of another value.

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

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines addresses, &, *, copy semantics, and absent pointer arithmetic, then applies them to flags, clients, and timeout errors.
Examples
Uses flag.StringVar, http.DefaultClient, private HTTP clients, and *url.Error timeout checks.
What it assumes
Assumes familiarity with Go variables and functions while teaching address and indirection operators through flag parsing.
What you can do after
Apply pointers via flag.StringVar and diagnose pointer-bearing timeout errors with errors.As.
How it connects
Links pointers with command-line flags, pass-by-value, indirection, HTTP clients, and error handling.

Also explained in

Compare how 17 books explain reference →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.