1.5. Stack—Custom Types with Methods
Although Go supports object-oriented programming it provides neither classes nor inheritance (is-a relationships). Go does support the creation of custom types, and Go makes aggregation (has-a relationships) extremely easy. Go also allows for the complete separation of a type’s data from its behavior, and supports duck typing. Duck typing is a powerful abstraction mechanism that means that values can be handled (e.g., passed to functions), based on the methods they provide, regardless of their actual types. The terminology is derived from the phrase, “If it walks like a duck, and quacks like a duck, it is a duck”. All of this produces a more flexible and powerful alternative to the classes and inheritance approach—but does require those of us used to the more traditional approach to make some significant conceptual adjustments to really benefit from Go’s object orientation.
Go represents data using the fundamental built-in types such as keyword!!struct bool, int, and string, or by aggregations of types using structs.6 Go’s custom types are based on the fundamental types, or on structs, or on other custom types. (We will see some simple examples later in this chapter; §1.7, → 40.)
Go supports both named and unnamed custom types. Unnamed types with the same structure can be used interchangeably; however, they cannot have any methods. (We will discuss this more fully in §6.4, → 273.) Any named custom type can have methods and these methods together constitute the type’s interface. Named custom types—even with the same structure—are not interchangeable. (Throughout the book any reference to a “custom type” means a named custom type, unless stated otherwise.)
An interface is a type that can be formally defined by specifying a particular set of methods. Interfaces are abstract and cannot be instantiated. A concrete (i.e., noninterface) type that has the methods specified by an interface fulfills the interface, that is, values of such a concrete type can be used as values of the interface’s type as well as of their own actual type. Yet no formal connection need be established between an interface and a concrete type that provides the methods specified by the interface. It is sufficient for a custom type to have the interface’s methods for it to satisfy that interface. And, of course, a type can satisfy more than one interface simply by providing all the methods for all the interfaces we want it to satisfy.
The empty interface (i.e., the interface that has no methods) is specified as interface{}. 7 Since the empty interface makes no demands at all (because it doesn’t require any methods), it can stand for any value (in effect like a pointer to any value), whether the value is of a built-in type or is of a custom type. (Go’s pointers and references are explained later; §4.1, → 138.) Incidentally, in Go terminology we talk about types and values rather than classes and objects or instances (since Go has no classes).
Function and method parameters can be of any built-in or custom type—or of any interface type. In the latter case this means that a function can have a parameter that says, for example, “pass a value that can read data”, regardless of what that value’s type actually is. (We will see this in practice shortly; §1.6, → 29.)
Chapter 6 covers all of these matters in detail and presents many examples to ensure that the ideas are understood. For now, let’s just look at a very simple custom type—a stack—starting with how values are created and used, and then looking at the implementation of the custom type itself.
We will start with the output produced by a simple test program:
Each item was popped from the custom stack and printed on its own line.
The simple test program that produced this output is stacker/stacker.go. Here are the imports it uses:
The fmt package is part of Go’s standard library, but the stack package is a local package specific to the stacker application. A Go program or package’s imports are first searched for under the GOPATH path or paths, and then under GOROOT. In this particular case the program’s source code is in $HOME/goeg/src/stacker/stacker.go and the stack package is in $HOME/goeg/src/stacker/stack/stack.go. The go tool will build both of them so long as the GOPATH is (or includes) the path $HOME/goeg/.
Import paths are specified using Unix-style “/”s, even on Windows. Every local package should be stored in a directory with the same name as the package. Local packages can have their own packages (e.g., like path/filepath), in exactly the same way as the standard library. (Creating and using custom packages is covered in Chapter 9.)
Here’s the simple test program’s main() function that produced the output:
The function begins by declaring the haystack variable of type stack.Stack. It is conventional in Go to always refer to types, functions, variables, and other items in packages using the syntax pkg.item, where pkg is the last (or only) component of the package’s name. This helps prevent name collisions. We then push some items onto the stack and then pop them off and print each one until there are no more left.
One amazingly convenient aspect of our custom stack is that despite Go’s strong typing, we are not limited to storing homogeneous items (items all of the same type), but can freely mix heterogeneous items (items of various types). This is because the stack.Stack type simply stores interface{} items (i.e., values of any type) and doesn’t care what their types actually are. Of course, when those items are used, then their type does matter. Here, though, we only use the fmt.Println() function and this uses Go’s introspection facilities (from the reflect package) to discover the types of the items it is asked to print. (Reflection is covered in a later chapter; §9.4.9, → 425.)
Another nice Go feature illustrated by the code is the forloop with no conditions. This is an infinite loop, so in most situations we will need to provide a means of breaking out of the loop—for example, using a break statement as here, or a return statement. We will see an additional for syntax in the next example (§1.6, → 29); the complete range of for syntaxes is covered in Chapter 5.
Go functions and methods can return a single value or multiple values. It is conventional in Go to report errors by returning an error value (of type error) as the last (or only) value returned by a function or method. The custom stack. Stack type respects this convention.
Now that we have seen the custom stack.Stack type in use we are ready to review its implementation (in file stacker/stack/stack.go).
The file starts conventionally by specifying its package name. Then it imports other packages that it needs—in this case just one, errors.
When we define a named custom type in Go what we are doing is binding an identifier (the type’s name) to a new type that has the same underlying representation as an existing (built-in or custom) type—and which is treated by Go as different from the underlying representation. Here, the Stack type is a new name for a slice (i.e., a reference to a variable-length array) of interface{} values—and is considered to be different from a plain []interface{}.
Because all Go types satisfy the empty interface, values of any type can be stored in a Stack.
The built-in collection types (maps and slices), communication channels (which can be buffered), and strings, can all return their length (or buffer size) using the built-in len() function. Similarly, slices and channels can also report their capacity (which may be greater than the length being used) using the built-in cap() function. (All of Go’s built-in functions are listed in Table 5.1; → 185, with cross-references to where they are covered; slices are covered in Chapter 4; §4.2, → 146.) It is conventional for custom collection types—our own, and those in the Go standard library—to support corresponding Len() and Cap() methods when these make sense.
Since the Stack type uses a slice for its underlying representation it makes sense to provide Stack.Len() and Stack.Cap() methods for it.
Both functions and methods are defined using the func keyword.However,in the case of methods the type of value to which the method applies is written after the func keyword and before the method’s name, enclosed in parentheses. After the function or method’s name comes a—possibly empty—parenthesized list of comma-separated parameters (each written in the form variableName type). After the parameters comes the function or method’s opening brace (if it has no return value), or a single return value (e.g., as a type name such as the int returned by the Stack.Len() method shown here), or a parenthesized list of return values, followed by an opening brace.
In most cases a variable name for the value on which the method is called is also given—as here where we have used the name stack (and with no conflict with the package’s name). The value on which the method is called is known in Go terminology as the receiver. 8
In this example the type of the receiver is Stack, so the receiver is passed by value. This means that any changes made to the receiver would be made on a copy of the original value and in effect lost. This is no problem for methods that don’t modify the receiver, such as the Stack.Len() method shown here.
The Stack.Cap() method is almost identical to the Stack.Len() method (and so is not shown). The only difference is that the Stack.Cap() method returns the cap() rather than the len() of the receiver stack. The source code also includes a Stack.IsEmpty() method, but this is so similar to Stack.Len()—it just returns a bool indicating whether the stack’s len() equals 0—that again it isn’t shown.
The Stack.Push() method is called on a pointer to a Stack (explained in a moment), and is passed a value (x) of any type. The built-in append() function takes a slice and one or more values and returns a (possibly new) slice which has the original slice’s contents, plus the given value or values as its last element or elements. (See §4.2.3, → 154.)
If the stack has previously had items popped from it (→ 28), the underlying slice’s capacity is likely to be greater than its length, so the push could be very cheap: simply a matter of putting the x item into the len(stack) position and increasing the stack’s length by one.
The Stack.Push() method always works (unless the computer runs out of memory), so we don’t need to return an error value to indicate success or failure.
If we want to modify a value we must make its receiver a pointer.9 A pointer is a variable that holds the memory address of another value. One reason that pointers are used is for efficiency—for example, if we have a value of a large type it is much cheaper to pass a pointer to the value as a parameter than to pass the value itself. Another use is to make a value modifiable. For example, when a variable is passed into a function the function gets a copy of the value (e.g., the stack passed into the stack.Len() function; 25←). This means that if we make any changes to the variable inside the function, they will have no effect on the original value. If we need to modify the original value—as here where we want to append to the stack—we must pass a pointer to the original value, and then inside the function we can modify the value that the pointer points to.
A pointer is declared by preceding the type name with a star (i.e., an asterisk, *). So here, in the Stack.Push() method, the stack variable is of type *Stack, that is, the stack variable holds a pointer to a Stack value and not an actual Stack value. We can access the actual Stack value that the pointer points to by dereferencing the pointer—this simply means that we access the value the pointer points to. Dereferencing is done by preceding the variable name with a star. So here, when we write stack we are referring to a pointer to a Stack (i.e., to a *Stack), and when we write *stack we are dereferencing the pointer, that is, referring to the actual Stack that the pointer points to.
So, in Go (and C and C++ for that matter), the star is overloaded to mean multiplication (when between a pair of numbers or variables, e.g., x*y), pointer declaration (when preceding a type name, e.g., z*MyType), and pointer dereference (when preceding a pointer variable’s name, e.g., *z). Don’t worry too much about these matters for now: Go’s pointers are fully explained in Chapter 4.
Note that Go’s channels, maps, and slices are all created using the make() function, and make() always returns a reference to the value it created. References behave very much like pointers in that when they are passed to functions any changes made to them inside the function affect the original channel, map, or slice. However, references don’t need to be dereferenced, so in most cases there’s no need to use stars with them. But if we want to modify a slice inside a func tion or method using append() (as opposed to simply changing one of its existing items), then we must either pass the slice by pointer, or return the slice (and set the original slice to the function or method’s return value), since append() sometimes returns a different slice reference than the one it was passed.
The Stack type uses a slice for its representation and therefore Stack values can be used with functions that operate on a slice, such as append() and len(). Nonetheless, Stack values are values in their own right, distinct from their representation, so they must be passed by pointer if we want to modify them.
The Stack.Top() method returns the item at the top of the stack (the item that was added last) and a nil error value; or a nil item and a non-nil error value, if the stack is empty. The stack receiver is passed by value since the stack isn’t modified.
The error type is an interface type (§6.3, → 263) which specifies a single method, Error() string. In general, Go’s library functions return an error as their last (or only) return value to indicate success (where error is nil) or failure. Here, we have made our Stack type work like a standard library type by creating a new error value using the errors package’s errors.New() function.
Go uses nil for zero pointers (and for zero references); that is, for pointers that point to nothing and for references that refer to nothing.10 Such pointers should be used only in conditions or assignments; methods should not normally be called on them.
Constructors are never called implicitly in Go. Instead Go guarantees that when a value is created it is always initialized to its zero value. For example, numbers are initialized to 0, strings to the empty string, pointers to nil, and the fields inside structs are similarly initialized. So there is no uninitialized data in Go, thus eliminating a major source of errors that afflicts many other programming languages. If the zero value isn’t suitable we can write a construction function—and call it explicitly—as we do here to create a new error. It is also possible to prevent values of a type being created without using a constructor function, as we will see in Chapter 6.
If the stack is nonempty we return its topmost value and a nilerror value. Since Go uses 0-based indexing the first element in a slice or array is at position 0 and the last element is at position len(sliceOrArray)-1.
There is no formality when returning more than one value from a function or method; we simply list the types we are returning after the function or method’s name and ensure that we have at least one return statement that has a corresponding list of values.
The Stack.Pop() method is used to remove and return the top (last added) item from the stack. Like the Stack.Top() method it returns the item and a nil error, or if the stack is empty, a nil item and a non-nil error.
The method must have a receiver that is a pointer since it modifies the stack by removing the returned item. For syntactic convenience, rather than referring to *stack (the actual stack that the stack variable points to) throughout the method, we assign the actual stack to a local variable (theStack), and work with that variable instead. This is quite cheap, because *stack is pointing to a Stack, which uses a slice for its representation, so we are really assigning little more than a reference to a slice.
If the stack is empty we return a suitable error. Otherwise we retrieve the stack’s top (last) item and store it in a local variable (x). Then we take a slice of the stack (which itself is a slice). The new slice has one less element than the original and is immediately set to be the value that the stack pointer points to. And at the end, we return the retrieved value and a nil error. We can reasonably expect any decent Go compiler to reuse the slice, simply reducing the slice’s length by one, while leaving its capacity unchanged, rather than copying all the data to a new slice.
The item to return is retrieved using the [] index operator with a single index (➊); in this case the index of the slice’s last element.
The new slice is obtained by using the [] slice operator with an index range (➋). An index range has the form first:end. If first is omitted—as here—0 is assumed, and if end is omitted, the len() of the slice is assumed. The slice thus obtained has elements with indexes from and including the first up to and excluding the end. So in this case, by specifying the last index as one less than the length, we slice up to the last but one element, effectively removing the last element from the slice. (Slice indexing is covered in Chapter 4; §4.2.1, → 151.)
In this example we used Stack receivers rather than pointers (i.e., of type *Stack) for those methods that don’t modify the Stack. For custom types with lightweight representations (say, a few ints or strings), this is perfectly reasonable. But for heavyweight custom types it is usually best to always use pointer receivers since a pointer is much cheaper to pass (typically a simple 32-or 64-bit value), than a large value, even for methods where the value isn’t modified.
A subtle point to note regarding pointers and methods is that if we call a method on a value, and the method requires a pointer to the value it is called on, Go is smart enough to pass the value’s address rather than a copy of the value (providing the value is addressable; §6.2.1, → 256). Correspondingly, if we call a method on a pointer to a value, and the method requires a value, Go is smart enough to dereference the pointer and give the method the pointed-to value.11
As this example illustrates, creating custom types in Go is generally straightforward, and doesn’t involve the cumbersome formalities that many other languages demand. Go’s object-oriented features are covered fully in Chapter 6.