Tour of Go - Methods and interfaces
Methods and interfaces
Methods
Go does not have classes. However, you can define methods on struct types.
The method receiver appears in its own argument list between the func
keyword and the method name.
Methods continued
You can declare a method on any type that is declared in your package, not just struct types.
However, you cannot define a method on a type from another package (including built in types).
Methods with pointer receivers
Methods can be associated with a named type or a pointer to a named type.
We just saw two Abs methods. One on the *Vertex
pointer type and the other on the MyFloat
value type.
There are two reasons to use a pointer receiver. First, to avoid copying the value on each method call (more efficient if the value type is a large struct). Second, so that the method can modify the value that its receiver points to.
Try changing the declarations of the Abs
and Scale
methods to use Vertex
as the receiver, instead of *Vertex
.
The Scale
method has no effect when v
is a Vertex
. Scale
mutates v
. When v
is a value (non-pointer) type, the method sees a copy of the Vertex
and cannot mutate the original value.
Abs
works either way. It only reads v
. It doesn’t matter whether it is reading the original value (through a pointer) or a copy of that value.
Interfaces
An interface type is defined by a set of methods.
A value of interface type can hold any value that implements those methods.
Note: There is an error in the example code on line 22. Vertex
(the value type) doesn’t satisfy Abser
because the Abs
method is defined only on *Vertex
(the pointer type).
Interfaces are satisfied implicitly
A type implements an interface by implementing the methods. There is no explicit declaration of intent; no “implements” keyword.
Implicit interfaces decouple implementation packages from the packages that define the interfaces: neither depends on the other.
It also encourages the definition of precise interfaces, because you don’t have to find every implementation and tag it with the new interface name.
Package io defines Reader
and Writer
; you don’t have to.
Stringers
One of the most ubiquitous interfaces is Stringer
defined by the fmt
package.
type Stringer interface {
String() string
}
A Stringer
is a type that can describe itself as a string. The fmt
package (and many others) look for this interface to print values.
Errors
Go programs express error state with error values.
The error type is a built-in interface similar to fmt.Stringer
:
type error interface {
Error() string
}
(As with fmt.Stringer
, the fmt
package looks for the error
interface when printing values.)
Functions often return an error
value, and calling code should handle errors by testing whether the error equals nil
.
i, err := strconv.Atoi("42")
if err != nil {
fmt.Printf("couldn't convert number: %v\n", err)
}
fmt.Println("Converted integer:", i)
A nil error
denotes success; a non-nil error denotes failure.
Readers
The io
package specifies the io.Reader
interface, which represents the read end of a stream of data.
The Go standard library contains many implementations of these interfaces, including files, network connections, compressors, ciphers, and others.
The io.Reader
interface has a Read
method:
func (T) Read(b []byte) (n int, err error)
Read
populates the given byte slice with data and returns the number of bytes populated and an error value. It returns an io.EOF
error when the stream ends.
The example code creates a strings.Reader
. and consumes its output 8 bytes at a time.
#Web servers#
Package http serves HTTP requests using any value that implements http.Handler
:
package http
type Handler interface {
ServeHTTP(w ResponseWriter, r *Request)
}
In this example, the type Hello
implements http.Handler
.
Visit http://localhost:4000/ to see the greeting.
Note: This example won’t run through the web-based tour user interface. To try writing web servers you may want to Install Go.
#Exercise: HTTP Handlers#
Implement the following types and define ServeHTTP
methods on them. Register them to handle specific paths in your web server.
type String string
type Struct struct {
Greeting string
Punct string
Who string
}
For example, you should be able to register handlers using:
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
Note: This example won’t run through the web-based tour user interface. To try writing web servers you may want to Install Go.
Images
Package image defines the Image
interface:
package image
type Image interface {
ColorModel() color.Model
Bounds() Rectangle
At(x, y int) color.Color
}
Note: the Rectangle
return value of the Bounds
method is actually an image.Rectangle
, as the declaration is inside package image
.
(See the documentation for all the details.)
The color.Color
and color.Model
types are also interfaces, but we’ll ignore that by using the predefined implementations color.RGBA
and color.RGBAModel
. These interfaces and types are specified by the image/color package
References
- Tour of Go,http://tour.golang.org/