concept pointer in category go

appears as: pointers, A pointer, pointer, pointer, pointers
Get Programming with Go

This is an excerpt from Manning's book Get Programming with Go.

Methods are similar to functions. You’ll learn all about them in lesson 13. The new built-in function is for pointers, which are covered in lesson 26.

Walk around any neighborhood and you’ll likely encounter homes with individual addresses and street signs to guide you on your way. You may happen upon a closed-down shop with an apologetic sign: “Sorry, we’ve moved!” Pointers are a bit like the sign in the store window that directs you to a different address.

A pointer is a variable that points to the address of another variable. In computer science, pointers are a form of indirection, and indirection can be a powerful tool.

Listing 26.4. Declaring a pointer: home.go
canada := "Canada"

var home *string
fmt.Printf("home is a %T\n", home)       #1

home = &canada
fmt.Println(*home)                       #2

26.6. Use pointers wisely

Pointers can be useful, but they also add complexity. It can be more difficult to follow code when values could be changed from multiple places.

Use pointers when it makes sense, but don’t overuse them. Programming languages that don’t expose pointers often use them behind the scenes, such as when composing a class of several objects. With Go you decide when to use pointers and when to not use them.

Go Web Programming

This is an excerpt from Manning's book Go Web Programming.

The handler function has two input parameters—a ResponseWriter interface and a pointer to a Request struct. It takes information from the Request to create an HTTP response, which is sent out through the ResponseWriter. The Fprintf function is called with the ResponseWriter, a format string with a single string format specifier (%s), followed by the path information extracted from the Request. Because you went to the address http://localhost:8080, there’s no path information and so nothing is printed out. If you’d gone to the address http://localhost:8080/sausheong/was/here, figure 1.4 shows how it would’ve looked in the browser.

So what exactly is a handler? We talked briefly about handlers and handler functions in chapters 1 and 2, so let’s elaborate here. In Go, a handler is an interface that has a method named ServeHTTP with two parameters: an HTTPResponseWriter interface and a pointer to a Request struct. In other words, anything that has a method called ServeHTTP with this method signature is a handler:

We talked about handlers, but what are handler functions? Handler functions are functions that behave like handlers. Handler functions have the same signature as the ServeHTTP method; that is, they accept a ResponseWriter and a pointer to a Request. The following listing shows how this works with our server.

Figure 8.4. Using the dependency injection pattern on the web service in chapter 7 by passing a pointer to a Post struct, which contains sql.Db, into the flow. The Post struct contains the sql.Db and so none of the functions in the flow are dependent on sql.Db.
Go in Action

This is an excerpt from Manning's book Go in Action.

Listing 2.29. feed.go: lines 17–36
17 // RetrieveFeeds reads and unmarshals the feed data file. 18 func RetrieveFeeds() ([]*Feed, error) { 19 // Open the file. 20 file, err := os.Open(dataFile) 21 if err != nil { 22 return nil, err 23 } 24 25 // Schedule the file to be closed once 26 // the function returns. 27 defer file.Close() 28 29 // Decode the file into a slice of pointers 30 // to Feed values. 31 var feeds []*Feed 32 err = json.NewDecoder(file).Decode(&feeds) 33 34 // We don't need to check for errors, the caller can do this. 35 return feeds, err 36 }

In the case of the Matcher interface, there’s only one method declared, Search, which takes a pointer to a value of type Feed and a search term of type string. The method also returns two values: a slice of pointers to values of type Result and an error value. The Result type is declared on lines 08 through 11.

Figure 4.5. An array of pointers that point to integers

There are two types of receivers in Go: value receivers and pointer receivers. In listing 5.13 on line 16, the notify method is declared with a value receiver.