learning-go
learning-go
Thanks to:
Authors: Go Authors
Miek Gieben Google
Go Nuts mailing list
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.
All example code used in ths book is hereby put in the public domain.
Learning as we Go.
Version: 0.4
Updated to Go release.2011-03-07.
Table of Contents
1 Introduction vi
Official documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Getting Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Origins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
2 Basics 1
Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Compiling and running code . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Variables, types and keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Operators and built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Go keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Built-in functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Arrays, slices and maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3 Functions 24
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Multiple return values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Named result parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Deferred code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Variadic parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Functions as values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Callbacks and closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Panic and recovering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4 Packages 42
Building a package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Documenting packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Testing packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Useful packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
6 Interfaces 66
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Interface names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
A sorting example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Introspection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7 Concurrency 78
More on channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
8 Communication 86
Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Command line arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Executing commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Netchan: networking and channels . . . . . . . . . . . . . . . . . . . . . . . . 89
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
9 Miscellaneous 98
Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Calling C from Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
A Colophon 100
Contributors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
License and copyright . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
B Index 102
C Bibliography 104
List of Figures
1.1 Chronology of Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
List of Tables
5.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
5.4 A generic map function in Go . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.5 A cat program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
6.1 Defining a struct and methods on it . . . . . . . . . . . . . . . . . . . . . . . 66
6.2 Another type that implements I . . . . . . . . . . . . . . . . . . . . . . . . . . 67
6.3 A function with a empty interface argument . . . . . . . . . . . . . . . . . . . 68
6.4 Failing to implement an interface . . . . . . . . . . . . . . . . . . . . . . . . 68
6.5 Failure extending built-in types . . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.6 Failure extending non-local types . . . . . . . . . . . . . . . . . . . . . . . . . 69
6.7 Dynamically find out the type . . . . . . . . . . . . . . . . . . . . . . . . . . 72
6.8 A more generic type switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.9 Introspection using reflection . . . . . . . . . . . . . . . . . . . . . . . . . . 73
6.10 Reflection and the type and value . . . . . . . . . . . . . . . . . . . . . . . . 74
6.11 Reflect with private member . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
6.12 Reflect with public member . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
7.1 Go routines in action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
7.2 Go routines and a channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
7.3 Using select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
7.4 Channels in Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.5 Adding an extra quit channel . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
7.6 A Fibonacci function in Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
8.1 Reading from a file (unbufferd) . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.2 Reading from a file (bufferd) . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.3 Processes in Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
8.6 uniq(1) in Perl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
8.4 Processes in Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
8.5 wc(1) in Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
8.7 uniq(1) in Go . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
8.8 Number cruncher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
List of Exercises
1 (1) Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
2 (1) For-loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3 (1) FizzBuzz . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4 (1) Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5 (4) Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
6 (4) Average . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
7 (3) Integer ordering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
8 (4) Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
9 (5) Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
10 (5) Var args . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
11 (5) Fibonacci . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
12 (4) Map function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
13 (3) Minimum and maximum . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
14 (5) Bubble sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
15 (6) Functions that return functions . . . . . . . . . . . . . . . . . . . . . . . . 33
16 (2) Stack as package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
17 (7) Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
List of Exercises v
This is an introduction to the Go language from Google. Its aim is to provide a guide to this
new and innovative language. What is Go? From the website [12]:
The intended audience of this book is people who are familiar with programming and
know multiple programming languages, be it C[23], C++[33], Perl [25], Java [24], Erlang[21],
Scala[4] or Haskell[13]. This is not a book which teaches you how to program, this is a book
that just teaches you how to use Go.
As with learning new things, probably the best way to do this is to discover it for your-
self by creating your own programs. Therefor includes each chapter a number of exercises
(and answers) to acquaint you with the language. An exercise is numbered as Qn, where
n is a number. After the exercise number another number in parentheses displays the
difficulty of this particular assignment. This difficulty ranges from 0 to 9, where 0 is easy
and 9 is difficult. Then a short name is given, for easier reference. For example
Official documentation
There already is a substantial amount of documentation written about Go. The Go Tutorial
[11], and the Effective Go document [6]. The website http://golang.org/doc/ is a very good
starting point for reading up on Goa . Reading these documents is certainly not required,
but is recommended.
Go comes with its own documentation in the form of a Go program called godoc. You
can use it yourself to look in the online documentation. For instance, suppose we want to
know more about the package hash. We would then give the command godoc hash. How
to create your own package documentation is explained in chapter 4.
Getting Go
There are currently (2011) no packages for Go in any Linux distribution. The route to
install Go is thus slightly longer than it could be. When Go stabilizes this situation will
change. For now, you need to retrieve the code from the mercurial archive and compile
Go yourself. For other Unix like systems the procedure is the same.
• First install Mercurial (to get the hg command). In Ubuntu/Debian/Fedora you must
install the mercurial package;
• For building Go you need the packages: bison, gcc, libc6-dev, ed, gawk and make;
• Set your PATH to so that the Shell can find the Go binaries:
% export PATH=$GOROOT/bin:$PATH
• Compile Go
% cd $GOROOT/src
% ./all.bash
a
http://golang.org/doc/ itself is served by a Go program called godoc.
viii Chapter 1: Introduction
You now have Go installed on your system and you can start playing.
Keeping up to date
New releases are announced on the Go Nuts mailing list [20]. To update an existing tree to
the latest release, you can run:
% cd $GOROOT
% hg pull
% hg update release
% cd src
% ./all.bash
% cd $GOROOT
% hg identify
79997f0e5823 release/release.2010-10-20
Origins
Go has it origins in Inferno [14] (which in turn was based upon Plan 9 [17]). Inferno in-
cluded a language called Limbo [16]. Quoting from the Limbo paper:
TODO One feature of Limbo that is included in Go is the support for cross compiling. Another
Alef language on Plan feature Go inherited from Limbo is channels (see chapter 7). Again from the Limbo docu-
9. Also presented in mentation.
1995.
[A channel] is a communication mechanism capable of sending and receiving
objects of the specified type to another agent in the system. Channels may be
used to communicate between local processes; using library procedures, they
may be connected to named destinations. In either case send and receive oper-
ations may be directed to them.
The channels in Go are easier to use than those in Limbo. If we dig even deeper in the his-
tory of Go we also find references to ”Newsqueak” [32], which pioneered the use of chan-
nel communication in a C–like language. Channel communication isn’t unique to these
languages, a big non–C–like language which also uses them is Erlang [21].
Exercises ix
The whole of idea of using channels to communicate with other processes is called
Communicating Sequential Processes (CSP) and was conceived by C. A. R. Hoare [27], who
incidentally is the same man that invented QuickSort [28].
Go is the first C–like language that is widely available, runs on many different
platforms and makes concurrency easy (or easier).
Exercises
Q1. (1) Documentation
1. Go’s documentation can be read with the godoc program, which is included the Go
distribution.
godoc hash gives information about the hash package. Reading the documentation
on container gives the following result:
SUBDIRECTORIES
heap
list
ring
vector
With which godoc command can you read the documentation of vector contained
in container?
Answers xi
Answers
There are a few things that make Go different from other languages.
Concurrent
Go makes it easy to ”fire off” functions to be run as very lightweight threads. These
threads are called goroutines a in Go;
Channels
Communication with these goroutines is done via channels [37][27];
Fast
Compilation is fast and execution is fast. The aim is to be as fast as C. Compilation
time is measured in seconds;
Safe
Go has garbage collection, no more free() in Go, the language takes care of this;
Standard format
A Go program can be formatted in (almost) any way the programmers want, but an
official format exist. The rule is very simple: The output of the filter gofmt is the
official endorsed format.
Postfix types
Types are given after the variable name, thus var a int, instead of int a; as one
would in C;
UTF-8
UTF-8 is everywhere, in strings and in the program code. Finally you can use Φ =
Φ + 1 in your source code;
Open Source
The Go license is completely open source, see the file LICENSE in the Go source code
distribution;
Fun
Programming with Go should be fun!
Erlang [21] also shares some of the features of Go. Notable differences between Erlang
and Go is that Erlang borders on being a functional language, where Go is an imperative
one. And Erlang runs in a virtual machine, while Go is compiled. Go also has a much more
Unix-like feeling to it.
a
Yes, that sounds a lot like coroutines, but goroutines are slightly different as we will see in chapter 7.
2 Chapter 2: Basics
Hello World
In the Go tutorial, Go is presented to the world in the typical manner: letting it print ”Hello
World” (Ken Thompson and Dennis Ritchie started this when they presented the C language
in the nineteen seventies). We don’t think we can do better, so here it is, ”Hello World” in
Go.
import "fmt" ..
// Implements formatted I/O. 1 3
.
/* Print something */ .2 5
..
func main() { 3 6
..
4 7
fmt.Printf("Hello, world; or καληµ´
ρα κóσµ; or こんにちは世界\n") 8
} 9
.. This first line is just required. All Go files start with package <something>, package
0
main is required for a standalone executable;
.. This says we need ”fmt” in addition to main. A package other than main is commonly
1
called a library, a familiar concept of many programming languages (see chapter 4).
The line ends with a comment which is started with //;
.. Just as package main was required to be first, import may come next. In Go, package
3
is always first, then import, then everything else. When your Go program is exe-
cuted, the first function called will be main.main(), which mimics the behavior from
C. Here we declare that function;
.. On line 8 we call a function from the package fmt to print a string to the screen. The
4
string is enclosed with " and may contain non-ASCII characters. Here we use Greek
and Japanese.
The Go compiler is named <number>g, where the number is 6 for 64 bit Intel and 8 for 32
bit Intel. The linker has a similar naming scheme: <number>l. In this book we will use 6g
and 6l for all the compiles. To compile the code from above, we use:
% 6g helloworld.go
% 6l helloworld.6
% ./6.out ← The default name for a (64 bit) Go executable (8.out on 32 bits)
Hello, world; or καληµ´
ρα κóσµ; or こんにちは世界
Using a Makefile
Another, less laborious (once setup), way to build a Go program, is to use a Makefile. The
following one can be used to build helloworld:
TARG=helloworld 3
GOFILES=\ 4
helloworld.go\ 5
include $(GOROOT)/src/Make.cmd 7
At line 3 you specify the name for your compiled program and on line 5 you enumerate
the source files. Now an invocation of make is enough to get your program compiled. Note
that Go ships with the variant of make, called gomake, which is (currently) a small wrapper
around GNU make. As the build system for Go programs may change in the future and
make make go away, we use gomake. Note that this Makefile creates an executable named
helloworld, not 6.out or 8.out.
In the next sections we will look at variables, basic types, keywords and control structures
of our new language. Go has a C-like feel when it comes to its syntax. If you want to put two
(or more) statements on one line, they must be separated with a semicolon (’;’). Normally
you don’t need the semicolon.
Go is different from other languages in that the type of a variable is specified after
the variable name. So not: int a, but a int. When declaring a variable it is assigned the
”natural” null value for the type. This means that after var a int, a has a value of 0. With
var s string, s is assigned the zero string, which is "".
Declaring and assigning in Go is a two step process, but they may be combined. Com-
pare the following pieces of code which have the same effect.
On the left we use the var keyword to declare a variable and then assign a value to
it. The code on the right uses := to do this in one step (this form may only be used inside
functions). In that case the variable type is deduced from the value. A value of 15 indicates
an int, a value of false tells Go that the type should be bool. Multiple var declarations
may also be grouped, const and import also allow this. Note the use of parentheses:
4 Chapter 2: Basics
var (
x int
b bool
)
Multiple variables of the same type can also be declared on a single line: var x, y int,
makes x and y both int variables. You can also make use of parallel assignment:
a, b := 20, 16
_, b := 34, 35
Declared, but otherwise unused variables are a compiler error in Go, the following code
generates this error: i declared and not used
package main
func main() {
var i int
}
Boolean types
A boolean type represents the set of boolean truth values denoted by the predeclared con-
stants true and false. The boolean type is bool.
Numerical types
Go has the well known types such as int, this type has the appropriate length for your
machine. Meaning that on a 32 bits machine they are 32 bits, and on a 64 bits machine
they are 64 bits. Note: an int is either 32 or 64 bits, no other values are defined. Same
goes for uint.
If you want to be explicit about the length you can have that too with int32, or uint32.
The full list for (signed and unsigned) integers is int8, int16, int32, int64 and byte, uint8,
uint16, uint32, uint64. With byte being an alias for uint8. For floating point values there
is float32 and float64 (there is no float type). A 64 bit integer or floating point value is
always 64 bit, also on 32 bit architectures.
Note however that these types are all distinct and assigning variables which mix these
types is a compiler error, like in the following code:
func main() { 3
var a int ← Generic integer type 4
var b int32 ← 32 bits integer type 5
a = 15 6
b = a + a ← Illegal mixing of these types 7
b = b + 5 ← 5 is a (typeless) constant, so this is OK 8
} 9
Variables, types and keywords 5
Constants
Constants in Go are just that — constant. They are created at compile time, and can only
be numbers, strings or booleans; const x = 42 makes x a constant. You can use iota b to
enumerate values.
const (
a = iota
b = iota
)
The first use of iota will yield 0, so a is equal to 0, whenever iota is used again on a new
line its value is incremented with 1, so b has a value of 1.
You can even do the following, let Go repeat the use of = iota:
const (
a = iota
b ← Implicitly b = iota
)
const (
a = 0 ← Is an int now
b string = "0"
)
Strings
s := "Hello World!"
Strings in Go are a sequence of UTF-8 characters enclosed in double quotes (”). If you use
the single quote (’) you mean one character (encoded in UTF-8) — which is not a string in
Go.
Once assigned to a variable the string can not be changed anymore: strings in Go are
immutable. For people coming from C, the following is not legal in Go:
s := "hello"
c := []byte(s) ..
0
b
The word [iota] is used in a common English phrase, ’not one iota’, meaning ’not the slightest difference’,
in reference to a phrase in the New Testament: ”until heaven and earth pass away, not an iota, not a dot, will pass
from the Law.” [39]
6 Chapter 2: Basics
c[0] = 'c' ..
1
s2 := string(c) .2.
..
fmt.Printf("%s\n", s2) 3
Multi-line strings
Due to the insertion of semicolons (see [6] section ”Semicolons”), you need to
be careful with using multiline strings. If you write:
s := "Starting part"
+ "Ending part"
s := "Starting part";
+ "Ending part";
s := "Starting part" +
"Ending part"
Then Go will not insert the semicolons in the wrong places. Another way would
be to use raw string literals by using back quotes: `:
s := `Starting part
Ending part`
Be aware that in this last example s now also contains the newline. Unlike inter-
preted string literals a raw string literal’s value is composed of the uninterpreted
characters between the quotes.
Complex numbers
Go has native support for complex numbers. If you use them you need a variable of the
type complex128 (64 bit imaginary part). If you want something smaller there is complex64
– for a 32 bits imaginary part. Complex numbers are written as re + imi, where re is
√
the real part, im is the imaginary part and i is the literal ’i’ ( −1). An example of using
complex numbers:
var c complex64 = 5+5i;fmt.Printf("Value is: %v", c)
will print: (5+5i)
Go supports the normal set of numerical operations, table 2.1 lists the current ones and
their relative precedence. They all associate from left to right.
Go keywords 7
Precedence Operator(s)
+ - * / and % all do what you would expect, & | ^ and &^ are bit operators for bitwise
and, bitwise or, bitwise xor and bit clear respectively. The && and || operators are
logical and and logical or. Not listed in the table is the logical not: ! .
Although Go does not support operator overloading (or method overloading for that
matter), some of the built-in operators are overloaded. For instance + can be used for
integers, floats, complex numbers and strings (adding strings is concatenating them).
Go keywords
Table 2.2 lists all the keywords in Go. In the following paragraphs and chapters we will
cover them. Some of these we have seen already.
• For var and const see section ”Variables, types and keywords” on page 3;
• package and import are briefly touch upon in section ”Hello World”. In chapter 4
they are documented in more detail.
• return is used to return from functions, for both func and return see chapter 3 for
the details;
Control structures
There are only a few control structures in Go c . For instance there is no do or while loop,
only a for. There is a (flexible) switch statement and if and switch accept an optional ini-
tialization statement like that of for. There also is something called a type switch and
a multiway communications multiplexer, select (see chapter 7). The syntax is differ-
ent (from that in C): parentheses are not required and the body must always be brace-
delimited.
If
if x > 0 { ←{ is mandatory
return y
} else {
return x
}
You can use the logical operators (see table 2.1) as you would normally do:
In the Go libraries, you will find that when an if statement doesn’t flow into the next
statement-that is, the body ends in break, continue, goto, or return, the unnecessary else
is omitted.
This is a example of a common situation where code must analyze a sequence of error
possibilities. The code reads well if the successful flow of control runs down the page,
eliminating error cases as they arise. Since error cases tend to end in return statements,
the resulting code needs no else statements.
c
This section is copied from [6].
Control structures 9
if err != nil
{ ← Must be on the same line as the if
return err
}
See [6] section ”Semicolons” for the deeper reasons behind this.
if err != nil {
return err
} else {
return nil
}
It will not compile. This is a bug in the Go compiler. See [19] for a extended
problem description and hopefully a fix.
Goto
Go has a goto statement — use it wisely. With goto you jump to a label which must be
defined within the current function. For instance a loop in disguise:
func myfunc() {
i := 0
Here: ← First word on a line ending with a colon is a label
println(i)
i++
goto Here ← Jump
}
For
The Go for loop has three forms, only one of which has semicolons.
Short declarations make it easy to declare the index variable right in the loop.
sum := 0
for i := 0; i < 10; i++ {
sum += i ← Short for sum = sum + i
} ←i ceases to exist after the loop
Finally, since Go has no comma operator and ++ and - - are statements not expressions, if
you want to run multiple variables in a for you should use parallel assignment.
// Reverse a
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 { ← Parallel assignment
a[i], a[j] = a[j], a[i] ← Here too
}
With break you can quit loops early, break breaks the current loop.
With loops within loops you can specify a label after break. Making the label identify which
loop to stop:
With continue you begin the next iteration of the loop, skipping any remaining code.
In the same way as break, continue also accepts a label. The following prints 0 to 5.
Range
The keyword range can be used for loops. It can loop over slices, arrays, strings, maps and
channels (see chapter 7). range is an iterator, that when called, returns a key-value pair
from the thing it loops over. Depending on what that is, range returns different things.
When looping over a slice or array range returns the index in the slice as the key and
value belonging to that index. Consider this code:
.. Create a slice (see ”Arrays, slices and maps” on page 14)) of strings.
0
.. Use range to loop over them.With each iteration range will return the index as int
1
and the key as a string, starting with 0 and ”a”.
.. k will have the value 0…5, and v will loop through ”a”…”f”.
2
You can also use range on strings directly. Then it will break out the individual Unicode
characters d and their start position, by parsing the UTF-8. The loop:
prints
Switch
Go’s switch is very flexible. The expressions need not be constants or even integers, the
cases are evaluated top to bottom until a match is found, and if the switch has no expres-
sion it switches on true. It’s therefore possible – and idiomatic – to write an if-else-if-
else chain as a switch.
There is no automatic fall through, you can however use fallthrough to make do just that.
Without fallthrough:
switch i {
case 0: // empty case body
case 1:
f() // f is not called when i == 0!
}
And with:
switch i {
case 0: fallthrough
case 1:
f() // f is called when i == 0!
}
With default you can specify an action when none of the other cases match.
switch i {
case 0:
case 1:
f()
default:
g() // called when i is not 0 or 1
}
Here’s a comparison routine for byte arrays that uses two switch statements:
Built-in functions
A small number of functions are predefined, meaning you don’t have to include any pack-
age to get access to them. Table 2.3 lists them all.
close and closed are used in channel communication and the closing of those channels,
see chapter 7 for more on this.
len and cap are used on a number of different types, len is used for returning the length
of strings and the length of slices and arrays. See section ”Arrays, slices and maps” for the
details of slices and arrays and the function cap.
new is used for allocating memory for user defined data types. See section ”Allocation
with new” on page 55.
make is used for allocating memory for built-in types (maps, slices and channels). See
section ”Allocation with make” on page 55.
copy is used for copying slices. append is for concatenating slices. See section ”Slices” in
this chapter.
panic and recover are used for an exception mechanism. See the section ”Panic and re-
covering” on page 31 for more.
print and println are low level printing functions that can be used without reverting to
the fmt package. These are mainly used for debugging.
complex, real and imag all deal with complex numbers. Other than the simple example
we gave, we will not further explain complex numbers.
14 Chapter 2: Basics
Storing multiple values in a list can be done by utilizing arrays, or their more flexible
cousin: slices. A dictionary or hash type is also available, it is called a map in Go.
Arrays
An array is defined by: [n]<type>, where n is the length of the array and <type> is the
stuff you want to store. Assigning, or indexing an element in the array is done with square
brackets:
Array types like var arr = [10]int have a fixed size. The size is part of the type. They can’t
grow, because then they would have a different type. Also arrays are values: Assigning one
array to another copies all the elements. In particular, if you pass an array to a function,
it will receive a copy of the array, not a pointer to it.
To declare an array you can use the following: var a [3]int, to initialize it to some-
thing else than zero, use a composite literal: a := [3]int{1, 2, 3} and this can be short-
A composite literal ened to a := [...]int{1, 2, 3}, where Go counts the elements automatically. Note that
allows you to assign all fields must be specified. So if you are using multidimensional arrays you have to do
a value directly to an quite some typing:
array, slice or map.
a := [2][2]int{ [2]int{1,2}, [2]int{3,4} }
When declaring arrays you always have to type something in between the square brackets,
Go release.2010-10- either a number or three dots (...) when using a composite literal. Since release.2010-10-27
27 [10]. this syntax was further simplified. From the release notes:
The syntax for arrays, slices, and maps of composite literals has been simpli-
fied. Within a composite literal of array, slice, or map type, elements that are
themselves composite literals may elide the type if it is identical to the outer
literal’s element type.
Slices
A slice is similar to an array, but it can grow when new elements are added. A slice always
refers to an underlaying array. What makes slices different from arrays is that a slice is
Reference types are a pointer to an array; slices are reference types, which means that if you assign one slice
created with make. to another, both refer to the same underlying array. For instance, if a function takes a
slice argument, changes it makes to the elements of the slice will be visible to the caller,
analogous to passing a pointer to the underlying array. With:
Arrays, slices and maps 15
sl := make([]int, 10)
you create a slice which can hold ten elements. Note that the underlaying array isn’t spec-
ified. A slice is always coupled to an array that has a fixed size. For slices we define a
capacity and a length. Figure 2.1 depicts the following Go code. First we create an array
of m elements of the type int: var array[m]int
Next, we create a slice from this array: slice := array[0:n]
And now we have:
• len(slice)== n == cap(slice)== n ;
• len(array)== cap(array)== m .
len == cap == m
len == cap == n
Given an array, or another slice, a new slice is created via a[I:J]. This creates a new
slice which refers to a, starts at index I, and ends before index J. It has length J - I.
.. Create a slice with all the elements of the array in it. This is a shorthand for: a[0:len(a)];
3
.. Create a slice with the elements from index 0 to 3, this is thus short for: a[0:4], and
4
yields: 1, 2, 3, 4;
.. Create a slice from the slice s2, note that s5 still refers to the array a.
5
16 Chapter 2: Basics
In the code listed in 2.6 we dare to do the impossible on line 8 and try to allocate some-
thing beyond the capacity (maximum length of the underlaying array) and we are greeted
with a runtime error.
func main() { 3
var array [100]int // Create array, index from 0 to 99 4
slice := array[0:99] // Create slice, index from 0 to 98 5
slice[98] = 'a' // OK 7
slice[99] = 'a' // Error: "throw: index out of range" 8
} 9
If you want to extend a slice, there are a couple of built-in functions that make life easier:
append and copy. From [8]:
The function append appends zero or more values x to a slice s and returns the
resulting slice, with the same type as s. If the capacity of s is not large enough to
fit the additional values, append allocates a new, sufficiently large slice that fits
both the existing slice elements and the additional values. Thus, the returned
slice may refer to a different underlying array.
s0 := []int{0, 0}
s1 := append(s0, 2) ..
0
s2 := append(s1, 3, 5, 7) .1.
s3 := append(s2, s0...) ..
2
And
The function copy copies slice elements from a source src to a destination dst
and returns the number of elements copied. Source and destination may over-
lap. The number of arguments copied is the minimum of len(src) and len(dst).
var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
var s = make([]int, 6)
n1 := copy(s, a[0:]) ← n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
n2 := copy(s, s[2:]) ← n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
Exercises 17
Maps
Many other languages have a similar type built-in, Perl has hashes, Python has its dictio-
naries and C++ also has maps (in lib) for instance. In Go we have the map type. A map can be
thought of as an array indexed by strings (in its most simple form). In the following listing
we define a map which converts from a string (month abbreviation) to an int – the number
of days in that month. The generic way to define a map is with: map[<from type>]<to type>
monthdays := map[string]int{
"Jan": 31, "Feb": 28, "Mar": 31,
"Apr": 30, "May": 31, "Jun": 30,
"Jul": 31, "Aug": 31, "Sep": 30,
"Oct": 31, "Nov": 30, "Dec": 31, ← The comma here is required
}
year := 0
for _, days := range monthdays { ← Key is not used, hence _, days
year += days
}
fmt.Printf("Numbers of days in a year: %d\n", year)
Which looks a bit like the reverse of the ”comma ok” form.
Exercises
Q2. (1) For-loop
1. Create a simple loop with the for construct. Make it loop 10 times and print out
the loop counter with the fmt package.
2. Rewrite the loop from 1. to use goto. The keyword for may not be used.
3. Rewrite the loop again so that it fills an array and then prints that array to the
screen.
18 Chapter 2: Basics
Answers
package main
import "fmt"
func main() {
for i := 0; i < 10; i++ { ← See section For on page 9
fmt.Printf("%d\n", i)
}
}
2. Rewriting the loop results in code that should look something like this (only show-
ing the main-function):
func main() {
i := 0 ← Define our loop variable
I: ← Define a label
fmt.Printf("%d\n", i)
i++
if i < 10 {
goto I ← Jump back to the label
}
}
func main() {
var arr [10]int ← Create an array with 10 elements
for i := 0; i < 10; i++ {
arr[i] = i ← Fill it one by one
}
fmt.Printf("%v", arr) ← With %v Go prints the type
}
20 Chapter 2: Basics
You could even do this in one fell swoop by using a composite literal:
package main
import "fmt"
func main() {
const (
.
FIZZ = 3 .0
BUZZ = 5
)
var p bool ..
1
.. ;
for i := 1; i < 100; i++ { 2
p = false
..
if i%FIZZ == 0 { 3
fmt.Printf("Fizz")
p = true
}
..
if i%BUZZ == 0 { 4
fmt.Printf("Buzz")
p = true
}
.
if !p { .5
fmt.Printf("%v", i)
}
..
fmt.Println() 6
}
}
.. Define two constants to make the code more readable. See section ”Con-
0
stants”;
.
.1 Holds if we already printed someting;
.4. And if divisble by BUZZ, print ”Buzz”. Note that we have also taken care of
the FizzBuzz case;
.
.5 If neither FIZZ nor BUZZ printed, print the value;
package main
import "fmt"
func main() {
str := "A"
for i := 0; i < 100; i++ {
fmt.Printf("%s\n", str)
str = str + "A" ← String concatenation
}
}
2. To answer this question we need some help of the utf8 package. First we check
the documentation with godoc utf8 | less. When we read the documentation
we notice func RuneCount(p []byte)int. Secondly we can convert string to a byte
slice with
str := "hello"
b := []byte(str) ← Conversion, see page 59
import (
"fmt"
"utf8"
)
func main() {
str := "dsjkdshdjsdh....js"
fmt.Printf("String %s\nLenght: %d, Runes: %d\n", str,
len([]byte(str)), utf8.RuneCount([]byte(str)))
}
3. Reversing a string can be done as follows. We startfrom the left (i) and the right
(j) and swap the characters as we see them:
22 Chapter 2: Basics
func main() {
s := "foobar"
a := []byte(s) ← Again a conversion
// Reverse a
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
a[i], a[j] = a[j], a[i] ← Parallel assignment
}
fmt.Printf("%s\n", string(a)) ← Convert it back
}
sum := 0.0
switch len(xs) {
case 0: ..
0
ave = 0
default: ..
1
for _, v := range xs {
sum += v
}
.
ave = sum / float64(len(xs)) .2
}
RICHARD P. GABRIEL
Functions are the basic building blocks in Go programs; all interesting stuff happens in
them. A function is declared as follows:
.. A function can be defined to work on a specific type, a more common name for such
1
a function is method. This part is called a receiver and it is optional. This will be
used in chapter 6;
.. The variable q of type int is the input parameter. The parameters are passed pass-
3
by-value meaning they are copied. But be aware that reference types (slices, chan-
nels, maps and interfaces) are pass-by-reference even though you do not see the
pointers directly in the code;
.. The variables r and s are the named return parameters for this function. Note that
4
functions in Go can have multiple return values. See section ”Multiple return val-
ues” on page 26. If you want the return parameters not to be named you only give
the types: (int,int). If you have only one value to return you may omit the paren-
theses. If your function is a subroutine and does not have anything to return you
may omit this entirely;
.. This is the function’s body, note that return is a statement so the braces around the
5
parameter(s) are optional.
Here are a two examples, the left is a function without a return value, the one on the right
is a simple function that returns its input.
Scope 25
Functions can be declared in any order you wish, the compiler scans the entire file
before execution. So function prototyping is a thing of the past in Go. Go disallows nested
functions. You can however work around this by using anonymous functions, see section
”Functions as values” on page 30 in this chapter.
Recursive functions just work as in other languages:
This prints 9 8 7 6 5 4 3 2 1 0.
Scope
Variables declared outside any functions are global in Go, those defined in functions are
local to those functions. If names overlap — a local variable is declared with the same
name as a global one — the local variable hides the global one when the current function
is executed.
var a = 6 var a = 6
In listing 3.3 we introduce a local variable a in the function q(). This local a is only
26 Chapter 3: Functions
visible in q(). That is why the code will print: 656. In listing 3.4 no new variables are
introduced, there is only a global a. Assigning a new value to will be globally visible. This
code will print: 655
In the following example we call g() from f():
var a int
func main() {
a = 5
println(a)
f()
}
func f() {
a := 6
println(a)
g()
}
func g() {
println(a)
}
The printout will be: 565. A local variable is only valid when we are executing the
function in which it is defined.
One of Go’s unusual features is that functions and methods can return multiple values
(Python can do this too). This can be used to improve on a couple of clumsy idioms in C
programs: in-band error returns (such as -1 for EOF) and modifying an argument. In Go,
Write returns a count and an error: ”Yes, you wrote some bytes but not all of them because
you filled the device”. The signature of *File.Write in package os is:
and as the documentation says, it returns the number of bytes written and a non-nil Error
when n != len(b). This is a common style in Go.
A similar approach obviates the need to pass a pointer to a return value to simulate a
reference parameter. Here’s a simple-minded function to grab a number from a position
in a byte array, returning the number and the next position.
You could use it to scan the numbers in an input array a like this:
Without having tuples as a native type, multiple return values is the next best thing to
have. You can return precisely what you want without overloading the domain space with
special values to signal errors.
The return or result parameters of a Go function can be given names and used as regular
variables, just like the incoming parameters. When named, they are initialized to the zero
values for their types when the function begins; if the function executes a return statement
with no arguments, the current values of the result parameters are used as the returned
values. Using this features enables you (again) to do more with less code a .
The names are not mandatory but they can make code shorter and clearer: they are
documentation. If we name the results of nextInt it becomes obvious which returned int
is which.
Because named results are initialized and tied to an unadorned return, they can simplify
as well as clarify. Here’s a version of io.ReadFull that uses them well:
In the following example we declare a simple function which calculates the factorial value Some text in this sec-
of a value x. tion comes from [18].
When we use named result values, the code is shorter and easier to read. You can also
write a function with multiple return values:
Deferred code
Suppose you have a function in which you open a file and perform various writes and
reads on it. In such a function there are often spots where you want to return early. If you
do that, you will need to close the file descriptor you are working on. This often leads to
the following code:
if failureY {
file.Close()
return false
}
file.Close()
return true
Deferred code 29
Here a lot of code is repeated. To overcome this Go has the defer statement. After defer
you specify a function which is called just before a return from the function is executed.
The code above could be rewritten as follows. This makes the function more readable,
shorter and puts the Close right next to the Open.
You can put multiple functions on the ”deferred list”, like this example from [6]:
Deferred functions are executed in LIFO order, so the above code prints: 4 3 2 1 0.
With defer you can even change return values, provided that you are using named
result parameters and a function literalb , i.e:
Or this example which makes it easier to understand why and where you need the braces:
In that (unnamed) function you can access any named return parameter:
b
A function literal is sometimes called a closure.
30 Chapter 3: Functions
Variadic parameters
Functions that take variadic parameters are functions that have a variable number of pa-
rameters. To do this, you first need to declare your function to take variadic arguments:
The arg ... int instructs Go to see this as a function that takes a variable number of ar-
guments. Note that these arguments all have the type int. Inside your function’s body the
variable arg is a slice of ints:
If you don’t specify the type of the variadic argument it defaults to the empty interface
interface{} (see chapter 6”). Suppose we have another variadic function called myfunc2,
the following example shows how to pass the variadic arguments to it:
Functions as values
As with almost everything in Go, functions are also just values. They can be assigned to
variables as follows:
Or you can write a function that takes a function as its parameter, for example a Map func-
tion that works on int slices. This is left as an exercise for the reader, see exercise Q12 on
page 33.
Callbacks and closures 31
With functions as values they are easy to pass to functions, from where they can be used
as callbacks. First define a function that does ”something” with an integer value:
The signature of this function is: func printit(int), or without the function name: func(int).
To create a new function that uses this one as a callback we need to use this signature:
We’ve already seen some use of closures in section ”Deferred code”, but there is more to
tell. When you define a closure, i.e. when you start using a function literal you still have
access to the (local) variables defined in the current function.
If you want to do this by not using a closure and defining a completely new function
you need to pass all vars to the function. TODO
Gist is there, but
needs much better
Panic and recovering wording and code
Go does not have a exception mechanism, like that in Java for instance: you can not throw
exceptions. Instead it using a panic-and-recover mechanism. It is worth remembering
that you should use this as a last resort, your code will not look, or be, better if it is littered
with panics. It’s a powerful tool: use it wisely. So, how do you use it.
The following description was taken from [5]:
Panic
is a built-in function that stops the ordinary flow of control and begins panicking.
When the function F calls panic, execution of F stops, any deferred functions in F are
executed normally, and then F returns to its caller. To the caller, F then behaves like
32 Chapter 3: Functions
a call to panic. The process continues up the stack until all functions in the current
goroutine have returned, at which point the program crashes.
Panics can be initiated by invoking panic directly. They can also be caused by run-
time errors, such as out-of-bounds array accesses.
Recover
is a built-in function that regains control of a panicking goroutine. Recover is only
useful inside deferred functions.
During normal execution, a call to recover will return nil and have no other effect.
If the current goroutine is panicking, a call to recover will capture the value given
to panic and resume normal execution.
Exercises
Q6. (4) Average
1. Write a function that calculates the average of a float64 slice.
Q7. (3) Integer ordering
1. Write a function that returns its (two) parameters in the right, numerical (ascend-
ing) order:
f(7,2) → 2,7
f(2,7) → 2,7
package main 1
import "fmt" 3
func main() { 5
for i := 0; i < 10; i++ { 6
fmt.Printf("%v\n", i) 7
} 8
fmt.Printf("%v\n", i) 9
} 10
i++ push(k)
k i--
i pop() k
l
0 m
Exercises 33
2. Bonus. Write a String method which converts the stack to a string representation.
This way you can print the stack using: fmt.Printf("My stack %v\n", stack)
The stack in the figure could be represented as: [0:m] [1:l] [2:k]
Q10. (5) Var args
1. Write a function that takes a variable numbers of ints and prints each integer on
a separate line
Q11. (5) Fibonacci
1. The Fibonacci sequence starts as follows: 1, 1, 2, 3, 5, 8, 13, . . . Or in mathemati-
cal terms: x1 = 1; x2 = 1; xn = xn−1 + xn−2 ∀n > 2.
Write a function that takes an int value and gives that many terms of the Fibonacci
sequence.
Q12. (4) Map function A map()-function is a function that takes a function and a list. The
function is applied to each member in the list and a new list containing these calculated
values is returned. Thus:
1. Write a simple map()-function in Go. It is sufficient for this function only to work
for ints.
2. Expand your code to also work on a list of strings.
Q13. (3) Minimum and maximum
1. Write a function that calculates the maximum value in an int slice ([]int).
2. Write a function that calculates the minimum value in a int slice ([]int).
Q14. (5) Bubble sort
1. Write a function that performs Bubble sort on slice of ints. From [36]:
p := plusTwo()
fmt.Printf("%v\n", p(2))
Which should print 4. See section Callbacks and closures on page 31 for informa-
tion about this topic.
2. Generalize the function from 1, and create a plusX(x) which returns a functions
that add x to an integer.
Answers 35
Answers
..
func average(xs []float64) (ave float64) { 0
sum := 0.0
switch len(xs) {
case 0: ..
1
ave = 0
default: ..
2
for _, v := range xs {
sum += v
}
..
ave = sum / float64(len(xs)) 3
}
return ..
4
}
func main() {
var i int
for i = 0; i < 10; i++ {
fmt.Printf("%v\n", i)
}
36 Chapter 3: Functions
fmt.Printf("%v\n", i)
}
Now i is defined outside the for-loop and still visible after wards. This code will
print the numbers 0 through 10.
Next we need the push and pop functions to actually use the thing. First we show
the wrong solution! In Go data passed to functions is passed-by-value meaning a
copy is created and given to the function. The first stab for the function push could
be:
The function works on the s which is of the type stack. To use this we just call
s.push(50), to push the integer 50 on the stack. But the push function gets a copy
of s, so it is not working the real thing. Nothing gets pushed to our stack this way,
for example the following code:
prints:
stack [0:0]
stack [0:0]
To solve this we need to give the function push a pointer to the stack. This means
we need to change push from
func (s stack)push(k int) → func (s *stack)push(k int)
We should now use new() (see ”Allocation with new” in chapter 5) to create a
pointer to a newly allocated stack, so line 1 from the example above needs to be
s := new(stack)
And our two functions become:
s.i++
}
func main() {
var s stack
s.push(25)
s.push(14)
fmt.Printf("stack %v\n", s)
}
2. While this was a bonus question, having the ability to print the stack was very valu-
able when writing the code for this exercise. According to the Go documentation
fmt.Printf("%v") can print any value (%v) that satisfies the Stringer interface.
For this to work we only need to define a String() function for our type:
package main
import "fmt"
func main() {
printthem(1, 4, 5, 7, 4)
printthem(1, 2, 4)
}
package main
import "fmt"
func main() {
.
for _, term := range fibonacci(10) { .4
fmt.Printf("%v ", term)
}
}
.. We create an array to hold the integers up to the value given in the function
0
call;
.
.1 Starting point of the Fibonicai calculation;
.4. Using the range keyword we ”walk” the numbers returned by the fibonacci
funcion. Here up to 10. And we print them.
return j
}
func main() {
m := []int{1, 3, 4}
f := func(i int) int {
return i * i
}
fmt.Printf("%v", (Map(f, m)))
}
The interested reader may combine max and min into one function with a selector
that lets you choose between the minimum or the maximum, or one that returns
both values.
func main() {
n := []int{5, -1, 0, 12, 3, 5}
fmt.Printf("unsorted %v\n", n)
// even though it's call by value, a slice is a
// reference type, so the underlaying array is changed!
bubblesort(n)
fmt.Printf("sorted %v\n", n)
}
1. func main() {
p2 := plusTwo()
fmt.Printf("%v\n",p2(2))
}
..
func plusTwo() func(int) int { 0
..
return func(x int) int { return x + 2 } 1
}
.. Define a new function that returns a function. See how you you can just
0
write down what you mean;
.
.1 Function literals at work, we define the +2–function right there in the return
statement.
..
func plusX(x int) func(int) int { 0
.
return func(y int) int { return x + y } .1
}
Packages are a collection of functions and data, they are like the Perl packages[15]. You
declare a package with the package keyword. The filename does not have to match the
package name. The convention for package names is to use lowercase characters. Go
packages may consist of multiple files, but they share the package <name> line. Lets define
a package even in the file even.go.
Names that start with a capital letter are exported and may be used outside your package,
more on that later. We can now use the package as follows in our own program myeven.go:
import ( ..
0
"./even" .1.
"fmt" ..
2
)
func main() {
i := 5
..
fmt.Printf("Is %d even? %v\n", i, even.Even(i)) 3
}
.. Use the function from the even package. The syntax for accessing a function from a
3
package is <package>.Function().
Building a package 43
Now we just need to compile and link, first the package, then myeven.go and then link
it:
% ./myeven
Is 5 even? false
In Go, a function from a package is exported (visible outside the package, i.e. public)
when the first letter of the function name is a capital, hence the function name Even. If we
change our myeven.go on line 7 to using to unexported function even.odd:
fmt.Printf("Is %d even? %v\n", i, even.odd(i))
We get an error when compiling, because we are trying to use a private function:
To summarize:
This convention also holds true for other names (new types, global variables) you define in
a package. Note that the term ”capital” is not limited to US ASCII, it extends into the entire
Unicode range. So captial Greek, Coptic, etc. is OK too.
Building a package
To create a package that other people can use (by just using import "even") we first need
to create a directory to put the package files in.
% mkdir even
% cp even.go even/
Next we can use the following Makefile, which is adapted for our even package.
TARG=even 3
GOFILES=\ 4
even.go\ 5
include $(GOROOT)/src/Make.pkg 7
Note that on line 3 we define our even package and on the lines 4 and 5 we enter the
files which make up the package. Also note that this is not the same Makefile setup as we
44 Chapter 4: Packages
used in section ”Using a Makefile” in chapter 2. The last line with the include statement is
different.
If we now issue gomake, a file named ”_go_.6”, a directory named ”_obj/” and a file
inside ”_obj/” called ”even.a” is created. The file even.a is a static library which holds the
compiled Go code. With gomake install the package (well, only the even.a) is installed in
the official package directory:
% make install
cp _obj/even.a $GOROOT/pkg/linux_amd64/even.a
Identifiers
Names are as important in Go as in any other language. In some cases they even have
semantic effect: for instance, the visibility of a name outside a package is determined by
whether its first character is upper case. It’s therefore worth spending a little time talking
about naming conventions in Go programs.
The convention that is used was to leave well-known legacy not-quite-words alone
rather than try to figure out where the capital letters go. Atoi, Getwd, Chmod. Camelcasing
works best when you have whole words to work with: ReadFile, NewWriter, MakeSlice.
Package names
When a package is imported (with import), the package name becomes an accessor for the
contents. After
import "bytes"
the importing package can talk about bytes.Buffer. It’s helpful if everyone using the pack-
age can use the same name to refer to its contents, which implies that the package name
should be good: short, concise, evocative. By convention, packages are given lower case,
single-word names; there should be no need for underscores or mixedCaps. Err on the
side of brevity, since everyone using your package will be typing that name. And don’t
worry about collisions a priori. The package name is only the default name for imports.
With the above import you can use bytes.Buffer. With
it becomes bar.Buffer. So it does need not be unique across all source code, and in the
rare case of a collision the importing package can choose a different name to use locally.
In any case, confusion is rare because the file name in the import determines just which
package is being used.
Documenting packages 45
Another convention is that the package name is the base name of its source directory;
the package in src/pkg/container/vector is imported as container/vector but has name vec-
tor, not container_vector and not containerVector.
The importer of a package will use the name to refer to its contents, so exported names
in the package can use that fact to avoid stutter. For instance, the buffered reader type
in the bufio package is called Reader, not BufReader, because users see it as bufio.Reader,
which is a clear, concise name. Moreover, because imported entities are always addressed
with their package name, bufio.Reader does not conflict with io.Reader. Similarly, the
function to make new instances of ring.Ring—which is the definition of a constructor in
Go—would normally be called NewRing, but since Ring is the only type exported by the
package, and since the package is called ring, it’s called just New. Clients of the package see
that as ring.New. Use the package structure to help you choose good names.
Another short example is once.Do; once.Do(setup) reads well and would not be im-
proved by writing once.DoOrWaitUntilDone(setup). Long names don’t automatically make
things more readable. If the name represents something intricate or subtle, it’s usually
better to write a helpful doc comment than to attempt to put all the information into the
name.
Finally, the convention in Go is to use MixedCaps or mixedCaps rather than under-
scores to write multiword names.
Documenting packages
This text is copied
Every package should have a package comment, a block comment preceding the package from [6].
clause. For multi-file packages, the package comment only needs to be present in one file,
and any one will do. The package comment should introduce the package and provide
information relevant to the package as a whole. It will appear first on the godoc page and
should set up the detailed documentation that follows. An example from the official regexp
package:
/*
The regexp package implements a simple library for
regular expressions.
regexp:
concatenation '|' concatenation
*/
package regexp
Each defined (and exported) function should have a small line of text documenting the
behavior of the function, from the fmt package:
Testing packages
In Go it is customary to write (unit) tests for your package. Writing tests involves the testing
package and the program gotest. Both have excellent documentation. When you include
46 Chapter 4: Packages
tests with your package keep in mind that has to be build using the standard Makefile (see
section ”Building a package”).
The testing itself is carried out with gotest. The gotest program run all the test func-
tions. Without any defined tests for our even package a gomake test, yields:
% gomake test
no test files found (*_test.go)
make: *** [test] Error 2
Let us fix this by defining a test in a test file. Test files reside in the package directory
and are named *_test.go. Those test files are just like other Go program, but gotest will
only execute the test functions. Each test function has the same signature and its name
should start with Test:
Fail marks the test function as having failed but continues execution.
FailNow marks the test function as having failed and stops its execution. Execution will
continue at the next test. So any other test in this file are skipped too.
Log formats its arguments using default formatting, analogous to Print(), and records the
text in the error log.
import "testing" 3
Note that we use package even on line 1, the tests fall in the same namespace as the pack-
age we are testing. This not only convenient, but also allows tests of unexported function
and structures. We then import the testing package and on line 5 we define the only test
function in this file. The displayed Go code should not hold any surprises: we check if the
Even function works OK. Now, the moment we’ve been waiting for, executing the test:
Useful packages 47
% gomake test
6g -o _gotest_.6 even.go even_test.go
rm -f _test/even.a
gopack grc _test/even.a _gotest_.6
PASS
Our test ran and reported PASS. Success! To show how a failed test look we redefine our
test function:
We now get:
And you can act accordingly (by fixing the test for instance).
Writing new packages should go hand in hand with writing (some) documen-
tation and test functions. It will make your code better and it shows that your
really put in the effort.
Useful packages
The standard Go repository includes a huge number of packages and it is even possible to
install more along side your current Go installation. It is very enlightening to browse the
$GOROOT/src/pkg directory and look at the packages. We cannot comment on each package,
but the following a worth a mention: a
fmt
Package fmt implements formatted I/O with functions analogous to C’s printf and
scanf. The format verbs are derived from C’s but are simpler. Some verbs (%-
sequences) that can be used:
%v
The value in a default format. when printing structs, the plus flag (%+v) adds
field names;
%#v
a Go-syntax representation of the value.
%T
a Go-syntax representation of the type of the value;
a
The descriptions are copied from the packages’ godoc. Extra remarks are type set in italic.
48 Chapter 4: Packages
io
This package provides basic interfaces to I/O primitives. Its primary job is to wrap
existing implementations of such primitives, such as those in package os, into shared
public interfaces that abstract the functionality, plus some other related primitives.
bufio
This package implements buffered I/O. It wraps an io.Reader or io.Writer object,
creating another object (Reader or Writer) that also implements the interface but
provides buffering and some help for textual I/O.
sort
The sort package provides primitives for sorting arrays and user-defined collec-
tions.
strconv
The strconv package implements conversions to and from string representations of
basic data types.
os
The os package provides a platform-independent interface to operating system func-
tionality. The design is Unix-like.
flag
The flag package implements command-line flag parsing. See ”Command line argu-
ments” on page 87.
json
The json package implements encoding and decoding of JSON objects as defined in
RFC 4627.
template
Data-driven templates for generating textual output such as HTML.
Templates are executed by applying them to a data structure. Annotations in the
template refer to elements of the data structure (typically a field of a struct or a key
in a map) to control execution and derive values to be displayed. The template walks
the structure as it executes and the ”cursor” @ represents the value at the current
location in the structure.
http
The http package implements parsing of HTTP requests, replies, and URLs and pro-
vides an extensible HTTP server and a basic HTTP client.
unsafe
The unsafe package contains operations that step around the type safety of Go pro-
grams. Normally you don’t need this package.
reflect
The reflect package implements run-time reflection, allowing a program to manip-
ulate objects with arbitrary types. The typical use is to take a value with static type
interface{} and extract its dynamic type information by calling Typeof, which re-
turns an object with interface type Type. That contains a pointer to a struct of type
*StructType, *IntType, etc. representing the details of the underlying type. A type
switch or type assertion can reveal which. See chapter 6, section ”Introspection”.
Exercises 49
exec
The exec package runs external commands.
Exercises
TODO
Q16. (2) Stack as package Write exercise for
1. See the Q9 exercise. In this exercise we want to create a separate package for that testing the stack
code. package. (MG)
Create a proper package for your stack implementation, Push, Pop and the Stack
type need to be exported.
2. Which official Go package could also be used for a (FIFO) stack?
Q17. (7) Calculator
1. Create a reverse polish calculator. Use your stack package.
2. Bonus. Rewrite your calculator to use the the package you found for question 2.
Answers 51
Answers
package stack
import (
"strconv"
)
package main
import (
"bufio"
"os"
"strconv"
"fmt"
52 Chapter 4: Packages
func main() {
for {
s, err := reader.ReadString('\n')
var token string
if err != nil {
return
}
for _, c := range s {
switch {
case c >= '0' && c <= '9':
token = token + string(c)
case c == ' ':
r, _ := strconv.Atoi(token)
st.push(r)
token = ""
case c == '+':
fmt.Printf("%d\n", st.pop()+st.pop())
case c == '*':
fmt.Printf("%d\n", st.pop()*st.pop())
case c == '-':
p := st.pop()
q := st.pop()
fmt.Printf("%d\n", q-p)
case c == 'q':
return
default:
//error
}
}
}
}
2. The container/vector package would be a good candidate. It even comes with Push
and Pop functions predefined. The changes needed to our program are minimal to
say the least, the following unified diff shows the differences:
Answers 53
func main() {
for {
Go has pointers. There is however no pointer arithmetic, so they act more like references
than pointers that you may know from C. Pointers are useful. Remember that when you
call a function in Go, the variables are pass-by-value. So, for efficiency and the possibility
to modify a passed value in functions we have pointers.
Just like in C you declare a pointer by prefixing the type with an ’*’: var p *int. Now p
is a pointer to an integer value. All newly declared variables are assigned their zero value
and pointers are no difference. A newly declared, or just a pointer that points to nothing
has a nil-value. In other languages this is often called a NULL pointer in Go it is just nil.
To make a pointer point to something you can use the address-of operator (&), which we
do on line 5:
More general: *X is a pointer to an X; [3]X is an array of three Xs. The types are therefore
really easy to read just read out the names of the type modifiers: [] declares an array slice;
’*’ declares a pointer; [size] declares an array. So []*[3]*X is an array slice of pointers to
arrays of three pointers to Xs (also see figure 5.1).
Figure 5.1. Pointers and types, the values v all have type X
*X v
[3]X [v0][v1][v2]
[k0] v0
[k1] v1
[k2] v2
Allocation
Go has garbage collection, meaning that you don’t have to worry about memory allocation
and deallocation. Of course almost every language since 1980 has this, but it is nice to see
garbage collection in a C-like language.
Go has two allocation primitives, new and make. They do different things and apply
to different types, which can be confusing, but the rules are simple. The following sec-
tions show how to handle allocation in Go and hopefully clarifies the somewhat artificial
distinction between new and make.
The built-in function new is essentially the same as its namesakes in other languages: new(T)
allocates zeroed storage for a new item of type T and returns its address, a value of type
*T. In Go terminology, it returns a pointer to a newly allocated zero value of type T. This is
important to remember:
This means a user of the data structure can create one with new and get right to work.
For example, the documentation for bytes.Buffer states that ”the zero value for Buffer is
an empty buffer ready to use.” Similarly, sync.Mutex does not have an explicit constructor
or Init method. Instead, the zero value for a sync.Mutex is defined to be an unlocked mutex.
The zero-value-is-useful property works transitively. Consider this type declaration.
See section ”Defining your own types” on page 57.
Values of type SyncedBuffer are also ready to use immediately upon allocation or just dec-
laration. In this snippet, both p and v will work correctly without further arrangement.
Back to allocation. The built-in function make(T, args) serves a purpose different from
new(T). It creates slices, maps, and channels only, and it returns an initialized (not zero)
value of type T, not *T. The reason for the distinction is that these three types are, under
56 Chapter 5: Beyond the basics
the covers, references to data structures that must be initialized before use. A slice, for
example, is a three-item descriptor containing a pointer to the data (inside an array), the
length, and the capacity; until those items are initialized, the slice is nil. For slices, maps,
and channels, make initializes the internal data structure and prepares the value for use.
For instance, make([]int, 10, 100) allocates an array of 100 ints and then creates a
slice structure with length 10 and a capacity of 100 pointing at the first 10 elements of the
array. In contrast, new([]int) returns a pointer to a newly allocated, zeroed slice structure,
that is, a pointer to a nil slice value.
These examples illustrate the difference between new and make.
// Unnecessarily complex:
var p *[]int = new([]int)
*p = make([]int, 100, 100)
// Idiomatic:
v := make([]int, 100)
Remember that make applies only to maps, slices and channels and does not return a pointer.
To obtain an explicit pointer allocate with new.
Sometimes the zero value isn’t good enough and an initializing constructor is necessary,
as in this example taken from the package os.
There’s a lot of boiler plate in there. We can simplify it using a composite literal, which is
an expression that creates a new instance each time it is evaluated.
It is OK to return the address of a local variable; the storage associated with the variable
survives after the function returns.
In fact, taking the address of a composite literal allocates a fresh instance each time it
is evaluated, so we can combine these last two lines.a
The fields of a composite literal are laid out in order and must all be present. However,
by labeling the elements explicitly as field:value pairs, the initializers can appear in any
order, with the missing ones left as their respective zero values. Thus we could say
As a limiting case, if a composite literal contains no fields at all, it creates a zero value for
the type. The expressions new(File) and &File{} are equivalent.
Composite literals can also be created for arrays, slices, and maps, with the field la-
bels being indices or map keys as appropriate. In these examples, the initializations work
regardless of the values of Enone, Eio, and Einval, as long as they are distinct.
Of course Go allows you to define new types, it does this with the type keyword:
Creates a new type foo which acts like an int. Creating more sophisticated types is done
with the struct keyword. An example would be when we want record somebody’s name
(string) and age (int) in a single structure and make it a new type:
import "fmt"
func main() {
a := new(NameAge)
a.name = "Pete"
a.age = 42
a
Taking the address of a composite literal tells the compiler to allocate it on the heap, not the stack.
58 Chapter 5: Beyond the basics
fmt.Printf("%v\n", a)
}
&{Pete 42}
That is nice! Go knows how to print your structure. If you only want to print one, or a
few, fields of the structure you’ll need to use .<field name>. For example to only print the
name:
struct {}
struct {
x, y int
_ float64 ← padding
A *[]int
F func()
}
If you omit the name for a field, you create an anonymous field, for instance:
struct {
T1 // field name is T1
*T2 // field name is T2
P.T3 // field name is T3
x, y int // field names are x and y
}
Note the field names that start with a capital letter are exported, i.e. can be set or read
from other packages. Field names that start with a lowercase are private to the current
package. The same goes for functions defined in packages, see chapter 4.
Methods
If you create functions that works on your newly defined type, you can take two routes:
2. Create a function that works on the type (see receiver in listing 3.1):
var n *NameAge
n.doSomething(2)
If x is addressable and &x’s method set contains m, x.m() is shorthand for (&x).
m().
In the above case this means that the following is not an error:
Here Go will search the method list for n of type NameAge, come up empty and will then also
search the method list for the type *NameAge and will translate this call to (&n).doSomething
(2).
Conversions
Sometimes you want to convert a type to another type. This is possible in Go, but there are
some rules. For starters, converting from one value to another is done by functions and
not all conversions are allowed.
To
[]byte × []byte(s)
[]int × []int(s)
string string(xb) string(xi) ×
float32 × float32(i)
int int(f) ×
byteslice := []byte(mystring)
Converts to a byte slice, each byte contains the integer value of the corresponding
byte in the string. Note that as strings in Go are encoded in UTF-8 some characters
in the string may end up in 1, 2, 3 or 4 bytes.
intslice := []int(mystring)
Converts to an int slice, each int contains a Unicode code point. Every character
from the string is corresponds to one integer.
• From floating point to an integer value: int(float32). This discards the fraction
part from the floating point value;
How can you convert between the types you have defined yourself? We create two types
here Foo and Bar, where Bar is an alias for Foo:
Then we:
Note the converting structures that are not identical in their fields is more difficult. Also
note that converting b to a plain int also fails; an integer is not the same as a structure
containing an integer.
Exercises
Q18. (6) Map function with interfaces
1. Use the answer from exercise Q12, but now make it generic using interfaces.
Q19. (6) Pointers
1. Suppose we have defined the following structure:
var p1 Person
p2 := new(Person)
Exercises 61
and
func Set(t T) {
x= &t
}
package main
import "container/vector"
func main() {
k1 := vector.IntVector{}
k2 := &vector.IntVector{}
k3 := new(vector.IntVector)
k1.Push(2)
k2.Push(3)
k3.Push(4)
}
Answers
1. package main
import "fmt"
func mult2(f e) e {
switch f.(type) {
case int:
return f.(int) * 2
case string:
return f.(string) + f.(string) + f.(string) + f.(string)
}
return f
}
func main() {
m := []e{1, 2, 3, 4}
s := []e{"a", "b", "c", "d"}
mf := Map(m, mult2)
sf := Map(s, mult2)
fmt.Printf("%v\n", mf)
fmt.Printf("%v\n", sf)
}
package main
..
0
import (
"os"
"fmt"
"bufio"
"flag"
)
..
var numberFlag = flag.Bool("n", false, "number each line") 1
..
2
func cat(r *bufio.Reader) {
i := 1
for {
buf, e := r.ReadBytes('\n') ..
3
if e == os.EOF { .4.
break
}
if *numberFlag { ..
5
fmt.Fprintf(os.Stdout, "%5d %s", i, buf)
i++
} else { ..
6
fmt.Fprintf(os.Stdout, "%s", buf)
}
}
return
}
func main() {
flag.Parse()
if flag.NArg() == 0 {
cat(bufio.NewReader(os.Stdin))
}
Answers 65
.1. Define a new flag ”n”, which defaults to off. Note that we get the help for
free;
.2. Start the function that actually reads the file’s contents and displays it;
.3. Read one line at the time;
2. The answer is given in [8] in the section ”Calls”, where among other things it says:
A method call x.m() is valid if the method set of (the type of) x contains
m and the argument list can be assigned to the parameter list of m. If
x is addressable and &x’s method set contains m, x.m() is shorthand for
(&x).m().
b
Also see section ”Methods” in this chapter.
6 Interfaces
eXistenZ
TED PIKUL
The following text is In Go, the word interface is overloaded to mean several different things. Every type has
from [34]. Written by an interface, which is the set of methods defined for that type. This bit of code defines a
Ian Lance Taylor — struct type S with one field, and defines two methods for S.
one of the authors of
Go. Listing 6.1. Defining a struct and methods on it
type S struct { i int }
func (p *S) Get() int { return p.i }
func (p *S) Put(v int) { p.i = v }
You can also define an interface type, which is simply a set of methods. This defines an
interface I with two methods:
type I interface {
Get() int
Put(int)
}
S is a valid implementation for interface I, because it defines the two methods which I re-
quires. Note that this is true even though there is no explicit declaration that S implements
I.
A Go program can use this fact via yet another meaning of interface, which is an inter-
face value:
func f(p I) { ..
0
..
fmt.Println(p.Get()) 1
.
p.Put(1) .2
}
Here the variable p holds a value of interface type. Because S implements I, we can call f
passing in a pointer to a value of type S:
var s S; f(&s)
Interfaces 67
The reason we need to take the address of s, rather than a value of type S, is because
we defined the methods on s to operate on pointers, see the code above in listing 6.1. This
is not a requirement — we could have defined the methods to take values — but then the
Put method would not work as expected.
The fact that you do not need to declare whether or not a type implements an inter-
face means that Go implements a form of duck typing[38]. This is not pure duck typing,
because when possible the Go compiler will statically check whether the type implements
the interface. However, Go does have a purely dynamic aspect, in that you can convert
from one interface type to another. In the general case, that conversion is checked at run-
time. If the conversion is invalid — if the type of the value stored in the existing interface
value does not satisfy the interface to which it is being converted — the program will fail
with a runtime error.
Interfaces in Go are similar to ideas in several other programming languages: pure ab-
stract virtual base classes in C++, typeclasses in Haskell or duck typing in Python. However
there is no other language which combines interface values, static type checking, dynamic
runtime conversion, and no requirement for explicitly declaring that a type satisfies an
interface. The result in Go is powerful, flexible, efficient, and easy to write.
Which is what?
The function f can now accept variables of type R and S. Suppose you need to know the
actual type in the function f. In Go you can figure that out by using a type switch.
func f(p I) {
..
switch t := p.(type) { 0
.
case *S: .1
..
case *R: 2
case S: ..
3
case R: .4.
..
default: 5
}
}
.. The type switch. Use (type) in a switch statement. We store the type in the variable
0
t;
Note that a type switch is the only way to figure out what type of an interface variable is.
Using (type) outside a switch is illegal.
Empty interface
Since every type satisfies the empty interface: interface{}. We can create a generic func-
tion which has an empty interface as its argument:
The return any.(I).Get() is the tricky bit in this function. The value any has type inter-
face{}, meaning no guarantee of any methods at all: it could contain any type. The .(I) is
a type assertion which converts any to an interface of type I. If we have that type we can
invoke the Get() function. So if we create a new variable of the type *S, we can just call
g(), because *S also implements the empty interface.
s = new(S)
fmt.Println(g(s));
The call to g will work fine and will print 0. If we however invoke g() with a value that
does not implement I we have a problem:
This compiles OK, but when we run this we get slammed with:
panic: interface conversion: int is not main.I: missing method Get
Which is completely true, the built-in type int does not have a Get() method.
In your code you want to prevent these kind of errors, therefor Go provides you with a
way to check if a variable implements a certain interface, again this uses a type assertion,
but now in an if statement.
if ok := any.(I); ok {
/* any implements the interface I */
}
Methods
Methods are functions that have an receiver (see chapter 3). You can define methods on
any type (except on non-local types, this includes built-in types: the type int can not have
methods). You can however make a new integer type with its own methods. For example:
Listing 6.5. Failure extending built-in types Listing 6.6. Failure extending non-local types
func (i int) Emit() { func (a *net.AddrError) Emit() {
fmt.Printf("%d", i) fmt.Printf("%v", a)
} }
An interfaces defines a set of methods. A method contains the actual code. In other words,
an interface is the definition and the methods are the implementation. So a receiver can
not be a defined for interface types, doing so results in a invalid receiver type ...
compiler error. The authoritative word from the language spec [8]:
The receiver type must be of the form T or *T where T is a type name. T is called
the receiver base type or just base type. The base type must not be a pointer or
interface type and must be declared in the same package as the method.
Pointers to interfaces
Creating a pointer to an interface value is a useless action in Go. It is in fact illegal to TODO
create a pointer to an interface value. The release notes for that release that made them Interfaces is not a
illegal leave no room for doubt: pointer, not reference
type
The language change is that uses of pointers to interface values no longer auto-
matically dereference the pointer. A pointer to an interface value is more often Go release.2010-10-
13.
a beginner’s bug than correct code.
Would copy standard input into a copy of buf, not into buf itself. This is almost never the
desired behavior.
Interface names
By convention, one-method interfaces are named by the method name plus the -er suffix:
Reader, Writer, Formatter etc.
70 Chapter 6: Interfaces
There are a number of such names and it’s productive to honor them and the function
names they capture. Read, Write, Close, Flush, String and so on have canonical signatures
and meanings. To avoid confusion, don’t give your method one of those names unless it
has the same signature and meaning. Conversely, if your type implements a method with
the same meaning as a method on a well-known type, give it the same name and signature;
Text copied from [6]. call your string-converter method String not ToString.
A sorting example
A version that sorts strings is identical except for the signature of the function:
Using this approach would lead to two functions, one for each type. By using interfaces
we can make this more generic.
Lets create a new function that will sort both strings and integers, something along the
lines of this non-working example:
..
func sort(i []interface{}) { 0
switch i.(type) { ..
1
case string: .2.
// ...
case int:
// ...
}
..
3
}
.. Using a type switch we find out what the actual type is of the input;
1
But when we call this function with sort([]int{1, 4, 5}), it fails with: cannot use i (type
[]int) as type []interface in function argument
This is because Go can not easily convert to a slice of interfaces. Just converting to an
The full mailing list interface is easy, but to a slice is much more costly. To keep a long story short: Go does not
discussion on this
subject can be found
at [29].
A sorting example 71
1. Define an interface type (called Sorter here) with a number of methods needed for
sorting. We will at least need a function to get the length of the slice, a function to
compare two values and a swap function;
2. Define new types for the slices we want to sort. Note that we declare slice types;
type Xi []int
type Xs []string
..
func Sort(x Sorter) { 0
..
for i := 0; i < x.Len() - 1; i++ { 1
for j := i + 1; j < x.Len(); j++ {
if x.Less(i, j) {
x.Swap(i, j)
}
}
}
}
Sort(ints)
fmt.Printf("%v\n", ints)
Sort(strings)
fmt.Printf("%v\n", strings)
Introspection
In a program, you can discover the dynamic type of an interface variable by using a switch.
Type assertion. Such a type assertion uses the syntax of a type assertion with the keyword type inside the
parentheses. If the switch declares a variable in the expression, the variable will have the
corresponding type in each clause.
.
type PersonShoe struct { .1
name string
shoesize int
}
func main() {
p1 := new(PersonAge)
p2 := new(PersonShoe)
WhichOne(p1)
WhichOne(p2)
}
..
func WhichOne(x interface{}) { 2
..
switch t := x.(type) { 3
case *PersonAge: ..
4
println("Age person")
case *PersonShoe:
println("Shoe person")
}
}
.. And PersonShoe;
1
.. This function must accept both types as valid input, so we use the empty Interface,
2
which every type implements;
.. When allocated with new, it’s a pointer. So we check for *PersonAge. If WhichOne()
4
was called with a non pointer value, we should check for PersonAge.
The following is another example of performing a type switch, but this time checking
for more (built-in) types:
In the following example we want to look at the ”tag” (here named ”namestr”) defined in
the type definition of Person. To do this we need the reflect package (there is no other way
in Go). Keep in mind that looking at a tag means going back the type definition. So we use
the reflect package to figure out the type of the variable and then access the tag.
.. On Value we use the function Type() which returns reflect.Type. We need to get
1
the type because there is where the tag is defined;
…which returns an object with interface type Type. That contains a pointer
to a struct of type *StructType, *IntType, etc. representing the details of
the underlying type. A type switch or type assertion can reveal which.
So we can access your specific type as a member of this struct. Which we do with
(*reflect.StructType);
.. A StructType has a number of methods, one of which is Field(n) which returns the
3
nth field of a structure. The type of this return is a StructField;
.. The struct StructField has a Tag member which returns the tag-name as a string. So
4
on the 0th field we can unleash .Tag to access this name: Field(0).Tag. This finally
gives us namestr.
To make the difference between looking a types and values more clear, that a look at
the following code:
.. Here we want to get the ”tag”, which means going for the type. Thus we need
0
Elem().Type().(*reflect.StructType) to get to it;
.. Now we want to get access to the value of one of the members and we employ
1
Elem().(*reflect.StructValue) to get to it. Now we have arrived at the structure.
Then we go the the first field Field(0), tell reflect is a *reflect.StringValue and
invoke the Get() method on it.
Exercises 75
Figure 6.1. Peeling away the layers using reflection. Going from
a *Person via *reflect.PtrValue using the methods described in
godoc reflect to get the actual string contained deep within.
*reflect.PtrValue
.Elem()
*reflect.Type
.(*reflect.StructValue)
*reflect.StructValue
*reflect.StructField
.FieldByName("Name")
*reflect.StringValue
.(*reflect.StringValue)
"Albert Einstein"
"Albert Einstein"
.Get()
Reflection works by peeling off layers once you have got your hands on a Value in
the reflection world.
Setting a value works similarly as getting a value, but only works on exported members.
Again some code:
Listing 6.11. Reflect with private member Listing 6.12. Reflect with public member
type Person struct { type Person struct {
name string "namestr" Name string "namestr" ←
age int age int
} }
The code on the left compiles and runs, but when you run it, you are greeted with a stack
trace and a runtime error:
panic: cannot set value obtained via unexported struct field
The code on the right works OK and sets the member Name to ”Albert Einstein”. Of course
this only works when you call Set() with a pointer argument.
Exercises
Q23. (6) Interfaces and compilation
1. The code in listing 6.4 on page 68 compiles OK — as stated in the text. But when
you run it you’ll get a runtime error, so something is wrong. Why does the code
76 Chapter 6: Interfaces
Answers
If g() is called now there are no run-time errors anymore. The idiom used is called
”comma ok” in Go.
Google IO 2010
ROBE PIKE
In this chapter we will show off Go’s ability for concurrent programming using channels
and goroutines. Goroutines are the central entity in Go’s ability for concurrency. But what
is a goroutine? From [6]:
A goroutine is a normal function, except that you start it with the keyword go.
The following idea for a program was taken from [31]. We run a function as two goroutines,
the goroutines wait for an amount of time and then print something to the screen. On the
lines 14 and 15 we start the goroutines. The main function waits long enough, so that both
goroutines will have printed their text. Right now we wait for 5 seconds (time.Sleep()
counts in ns) on line 17, but in fact we have no idea how long we should wait until all
goroutines have exited.
func main() { 13
go ready("Tee", 2) 14
go ready("Coffee", 1) 15
fmt.Println("I'm waiting") 16
time.Sleep(5 * 1e9) 17
} 18
If we did not wait for the goroutines (i.e. remove line 17) the program would be termi-
nated immediately and any running goroutines would die with it. To fix this we need some
kind of mechanism which allows us to communicate with the goroutines. This mechanism
is available to us in the form of channels. A channel can be compared to a two-way pipe
in Unix shells: you can send to and receive values from it. Those values can only be of a
specific type: the type of the channel. If we define a channel, we must also define the type
of the values we can send on the channel. Note that we must use make to create a channel:
ci := make(chan int)
cs := make(chan string)
cf := make(chan interface{})
Makes ci a channel on which we can send and receive integers, makes cs a channel for
strings and cf a channel for types that satisfy the empty interface. Sending on a channel
and receiving from it, is done with the same operator: <-. Depending on the operands it
figures out what to do:
func main() {
..
c = make(chan int) 2
..
go ready("Tee", 2) 3
go ready("Coffee", 1)
fmt.Println("I'm waiting, but not too long")
..
<-c 4
.
<-c .5
}
.. Declare c to be a variable that is a channel of ints. That is: this channel can move
0
integers. Note that this variable is global so that the goroutines have access to it;
.. Initialize c;
2
.. Wait until we receive a value from the channel. Note that the value we receive is
4
discarded;
80 Chapter 7: Concurrency
There is still some remaining ugliness; we have to read twice from the channel (lines 14 and
15). This is OK in this case, but what if we don’t know how many goroutines we started?
This is where another Go built-in comes in: select. With select you can (among other
things) listen for incoming data on a channel.
Using select in our program does not really make it shorter, because we run too few
goroutines. We remove the lines 14 and 15 and replace them with the following:
While our goroutines were running concurrent, they were not running in parallel. When
you do not tell Go anything there can only be one goroutine running at a time. With
runtime.GOMAXPROCS(n) you can set the number of goroutines that can run in parallel.
From the documentation:
GOMAXPROCS sets the maximum number of CPUs that can be executing si-
multaneously and returns the previous setting. If n < 1, it does not change the
current setting. This call will go away when the scheduler improves.
If you do not want to change any source code you can also set an environment variable
GOMAXPROCS to the desired value.
More on channels
When you create a channel in Go with ch := make(chan bool), an unbuffered channel for
bools is created. What does this mean for your program? For one, if you read (value
:= <-ch) it will block until there is data to receive. Secondly anything sending (ch<-5)
will block until there is somebody to read it. Unbuffered channels make a perfect tool for
synchronising multiple goroutines.
But Go allows you to specify the buffer size of a channel, which is quite simple how
many elements a channel can hold. ch := make(chan bool, 4), creates a buffered chan-
nels of bools that can hold 4 elements. The first 4 elements in this channels are written
without any blocking. When you write the 5th element, your code will block, until another
goroutine reads some elements from the channel to make room.
Although reads from channels block, you can perform an non-blocking read with the
TODO following syntax:
Still need to test this.
x, ok = <-ch
Exercises 81
Where ok is set to true when there was something to read (otherwise it is false. And if that
was the case x get the value read from the channel. In conclusion, the following is true in
Go:
{
value == 0 → unbuffered (blocking)
ch := make(chan type, value)
value > 0 → buffered (non-blocking) up to value elements)
Closing channels
See godns/resolver.go and how to wrap the Query() function in a goroutine with error
handling and such.
Exercises
Q26. (4) Channels
1. Modify the program you created in exercise Q2 to use channels, in other words,
the function called in the body should now be a goroutine and communication
should happen via channels. You should not worry yourself on how the goroutine
terminates.
2. There are a few annoying issues left if you resolve question 1. One of the problems
is that the goroutine isn’t neatly cleaned up when main.main() exits. And worse,
due to a race condition between the exit of main.main() and main.shower() not all
numbers are printed. It should print up until 9, but sometimes it prints only to 8.
Adding a second quit-channel you can remedy both issues. Do this.a
Q27. (7) Fibonacci II
1. This is the same exercise as the one given page 33 in exercise 11. For completeness
the complete question:
The Fibonacci sequence starts as follows: 1, 1, 2, 3, 5, 8, 13, . . . Or in
mathematical terms: x1 = 1; x2 = 1; xn = xn−1 + xn−2 ∀n > 2.
Write a function that takes an int value and gives that many terms of
the Fibonacci sequence.
But now the twist: You must use channels.
a
You will need the select statement.
Answers 83
Answers
package main 1
import "fmt" 3
func main() { 5
ch := make(chan int) 6
go shower(ch) 7
for i := 0; i < 10; i++ { 8
ch <- i 9
} 10
} 11
We start of in the usual way, then at line 6 we create a new channel of ints. In
the next line we fire off the function shower with the ch variable as it argument, so
that we may communicate with it. Next we start our for-loop (lines 8-10) and in
the loop we send (with <-) our number to the function (now a goroutine) shower.
In the function shower we wait (as this blocks) until we receive a number (line
15). Any received number is printed (line 16) and then continue the endless loop
started on line 14.
2. An answer is
package main 1
import "fmt" 3
func main() { 5
ch := make(chan int) 6
quit := make(chan bool) 7
go shower(ch, quit) 8
for i := 0; i < 10; i++ { 9
ch <- i 10
} 11
quit <- false // or true, does not matter 12
84 Chapter 7: Concurrency
} 13
On line 20 we read from the quit channel and we discard the value we read. We
could have used q := <-quit, but then we would have used the variable only once
— which is illegal in Go. Another trick you might have pulled out of your hat may
be: _ = <-quit. This is valid in Go, but the Go idiom favors the one given on line
20.
package main
import "fmt"
func dup3(in <-chan int) (<-chan int, <-chan int, <-chan int) {
a, b, c := make(chan int, 2), make(chan int, 2), make(chan int
, 2)
go func() {
for {
x := <-in
a <- x
b <- x
c <- x
}
}()
return a, b, c
}
}
}()
return out
}
func main() {
x := fib()
for i := 0; i < 10; i++ {
fmt.Println(<-x)
}
}
// See sdh33b.blogspot.com/2009/12/fibonacci-in-go.html
8 Communication
In this chapter we are going to look at the building blocks in Go for communicating with
the outside world.
Files
Reading from (and writing to) files is easy in Go. This program only uses the os package to
read data from the file /etc/passwd.
import "os" 3
func main() { 5
buf := make([]byte, 1024) 6
f, _ := os.Open("/etc/passwd", os.O_RDONLY, 0666) 7
defer f.Close() 8
for { 9
n, _ := f.Read(buf) 10
if n == 0 { break } 11
os.Stdout.Write(buf[0:n]) 12
} 13
} 14
func main() { 5
buf := make([]byte, 1024) 6
f, _ := os.Open("/etc/passwd", os.O_RDONLY, 0666) 7
defer f.Close() 8
r := bufio.NewReader(f) 9
w := bufio.NewWriter(os.Stdout) 10
defer w.Flush() 11
for { 12
n, _ := r.Read(buf) 13
if n == 0 { break } 14
Command line arguments 87
w.Write(buf[0:n]) 15
} 16
} 17
So anything that has such a Read() function implements this interface. And from listing
8.1 (line 10) we can see that *File indeed does so.
Arguments from the command line are available inside your program via the string slice
os.Args, provided you have imported the package os. The flag package has a more sophis-
ticated interface, and also provides a way to parse flags. Take this example from a little
DNS query tool:
..
var dnssec *bool = flag.Bool("dnssec", false, "Request DNSSEC records") 0
var port *string = flag.String("port", "53", "Set the query port") ..
1
flag.Usage = func() { ..
2
fmt.Fprintf(os.Stderr, "Usage: %s [@server] [qtype] [qclass] [name
...]\n", os.Args[0])
..
flag.PrintDefaults() 3
}
flag.Parse() ..
4
.. Define a bool flag, -dnssec. The variable must be a pointer otherwise the package
0
can not set its value;
.. For every flag given, PrintDefaults will output the help string;
3
Executing commands
The exec package has function to run external commands, and it the premier way to exe-
cute commands from within a Go program. We start commands with the Run function:
func Run(argv0 string, argv, envv []string, dir string, stdin, stdout,
stderr int) (p *Cmd, err os.Error)
Run starts the binary prog running with arguments argv and environment envv.
It returns a pointer to a new Cmd representing the command or an error.
88 Chapter 8: Communication
import "exec"
In the os package we find the StartProcess function. This is another way (but more low
level) to start executables.a The prototype for StartProcess is:
StartProcess starts a new process with the program, arguments, and environ-
ment specified by name, argv, and envv. The fd array specifies the file descriptors
to be set up in the new process: fd[0] will be Unix file descriptor 0 (standard
input), fd[1] descriptor 1, and so on. A nil entry will cause the child to have
no open file descriptor with that index. If dir is not empty, the child chdirs into
the directory before execing the program.
import "os"
Note that os.Wait (among other things) returns the exit code, with:
w := os.Wait(pid, os.WNOHANG)
e := w.WaitStatus.ExitStatus() ← ExitStatus() returns an integer
Networking
All network related types and functions can be found in the package net. One of the most
important functions in there is Dial. When you Dial into a remote system the function
returns a Conn interface type, which can be used to send and receive information. The
function Dial neatly abstracts away the network family and transport. So IPv4 or IPv6,
TCP or UDP can all share a common interface.
Dialing a remote system (port 80) over TCP, then UDP and lastly TCP over IPv6 looks
like this:b
Exercises
Q28. (8) Processes
1. Write a program that takes a list of all running processes and prints how many
child processes each parent has spawned. The output should look like:
Pid 0 has 2 children: [1 2]
Pid 490 has 2 children: [1199 26524]
Pid 1824 has 1 child: [7293]
• For acquiring the process list, you’ll need to capture the output of ps -e -opid,ppid,comm.
This output looks like:
PID PPID COMMAND
9024 9023 zsh
19560 9024 ps
• If a parent has one child you must print child, is there are more than one
print children;
• The process list must be numerically sorted, so you start with pid 0 and work
your way up.
Here is a Perl version to help you on your way (or to create complete and utter
confusion).
#!/usr/bin/perl -l
my (%child, $pid, $parent);
my @ps=`ps -e -opid,ppid,comm`; # Capture the ouput from `ps`
foreach (@ps[1..$#ps]) { # Discard the header line
($pid, $parent, undef) = split; # Split the line, discard 'comm'
push @{$child{$parent}}, $pid; # Save the child PIDs on a list
}
# Walk through the sorted PPIDs
foreach (sort { $a <=> $b } keys %child) {
print "Pid ", $_, " has ", @{$child{$_}}+0, " child", # Print
them
@{$child{$_}} == 1 ? ": " : "ren: ", "[@{$child{$_}}]";
}
#!/usr/bin/perl
my @a = qw/a b a a a c d e f g/;
print my $first = shift @a;
foreach (@a) {
if ($first ne $_) { print; $first = $_; }
}
or
(8 ∗ (75 + (8 ∗ 6))) − (7/1) = 977
1. Implement a number cruncher that works like that. Make it print the solution in
a similar format (i.e. output should be infix with parenthesis) as used above.
2. Calculate all possible solutions and show them (or only show how many there are).
In the example above there are 544 ways to do it.
Answers 91
Answers
package main 1
import ( 3
"os" 4
"fmt" 5
"sort" 6
"bufio" 7
"strings" 8
"strconv" 9
"container/vector" 10
) 11
const ( 13
PID = iota 14
PPID 15
) 16
func main() { 23
pr, pw, _ := os.Pipe() 24
defer pr.Close() 25
r := bufio.NewReader(pr) 26
w := bufio.NewWriter(os.Stdout) 27
defer w.Flush() 28
pid, _ := os.StartProcess("/bin/ps", []string{"ps", "-e", "- 29
opid,ppid,comm"}, nil, "", []*os.File{nil, pw, nil})
92 Chapter 8: Communication
child := make(map[int]*vector.IntVector) 33
s, ok := r.ReadString('\n') // Discard the header line 34
s, ok = r.ReadString('\n') 35
for ok == nil { 36
f := strings.Fields(s) 37
if _, present := child[atoi(f[PPID])]; !present { 38
v := new(vector.IntVector) 39
child[atoi(f[PPID])] = v 40
} 41
// Save the child PIDs on a vector 42
child[atoi(f[PPID])].Push(atoi(f[PID])) 43
s, ok = r.ReadString('\n') 44
} 45
package main
import (
"os"
"fmt"
"bufio"
"strings"
Answers 93
func main() {
var chars, words, lines int
..
r := bufio.NewReader(os.Stdin) 0
for {
..
switch s, ok := r.ReadString('\n'); true { 1
..
case ok != nil: 2
fmt.Printf("%d %d %d\n", chars, words, lines);
return
default: ..
3
chars += len(s)
words += len(strings.Fields(s))
lines++
}
}
}
package main
import "fmt"
func main() {
list := []string{"a", "b", "a", "a", "c", "d", "e", "f"}
first := list[0]
/* Go quine */
package main
import "fmt"
func main() {
fmt.Printf("%s%c%s%c\n", q, 0x60, q, 0x60)
}
var q = `/* Go quine */
package main
import "fmt"
func main() {
fmt.Printf("%s%c%s%c\n", q, 0x60, q, 0x60)
}
var q = `
package main
import (
"fmt"
"strconv"
"container/vector"
"flag"
)
const (
_ = 1000 * iota
ADD
SUB
MUL
DIV
MAXPOS = 11
)
var (
ok bool
value int
)
func main() {
flag.Parse()
list := []int{1, 6, 7, 8, 8, 75, ADD, SUB, MUL, DIV}
// list := []int1, 6, 7, ADD, SUB, MUL, DIV
magic, ok := strconv.Atoi(flag.Arg(0))
if ok != nil {
return
}
f := make([]int, MAXPOS)
solve(f, list, 0, magic)
}
if v < ADD {
// it is a number, save it
tmp = numberop[i]
numberop[i] = 0
}
form[index] = v
value, ok = rpncalc(form[0 : index+1])
if index == MAXPOS-1 {
if v < ADD {
numberop[i] = tmp // reset and go on
}
goto NEXT
}
solve(form, numberop, index+1, magic)
if v < ADD {
numberop[i] = tmp // reset and go on
}
NEXT:
}
}
Profiling
This section is heavily
inspired by [22].
Calling C from Go
A Colophon
This work was created with LaTEX. The main text is set in the Google Droid fonts. All type-
writer text is typeset in DejaVu Mono.
Contributors
The following people have helped to make this book what it is today.
• JC van Winkel.
Help with proof reading, checking exercises and text improvements (no particular or-
der): Filip Zaludek, Jonathan Kans, Jaap Akkerhuis, Mayuresh Kathe, Makoto Inoue, Ben
Bullock, Bob Cunningham, Dan Kortschak, Sonia Keys, Babu Sreekanth, Haiping Fan, Cecil
New, Andrey Mirtchovski, and Russel Winder.
Miek Gieben
buffered, 86 keyword
built-in break, 8, 10
append, 13, 16 continue, 10
cap, 13 default, 12
close, 13 defer, 28
closed, 13 else, 8
complex, 13 fallthrough, 12
copy, 13, 16 for, 9
imag, 13 go, 78
len, 13 goto, 9
make, 13, 55 if, 8
new, 13, 55 import, 44
panic, 13 iota, 5
print, 13 map, 17
println, 13 add elements, 17
real, 13 existence, 17
recover, 13 remove elements, 17
package, 42
channel, 79 range, 11, 17
blocking read, 80 on maps, 11, 17
blocking write, 80 on slices, 11
non-blocking read, 80 return, 8
non-blocking write, 80 select, 80
unbuffered, 80 struct, 57
channels, 1, 79 switch, 11
closure, 29 type, 57
complex numbers, 13
composite literal, 14 label, 9
generic, 70 operator
goroutine, 78 address-of, 54
goroutines, 1 and, 7
gotest, 45 bit wise xor, 7
Index 103
bitwise _, 4
and, 7 assigning, 3
clear, 7 declaring, 3
or, 7 underscore, 4
channel, 79
increment, 55
not, 7
or, 7
package
bufio, 45, 48, 86
bytes, 44
container/vector, 45
even, 42
exec, 48, 87
flag, 48
fmt, 13, 47
http, 48
io, 48
json, 48
lib, 17
os, 48, 88
reflect, 48, 73
ring, 45
sort, 48
strconv, 48
template, 48
unsafe, 48
parallel assignment, 4, 10
pass-by-reference, 24
pass-by-value, 24
private, 43
public, 43
receiver, 24
reference types, 14
runes, 11
scope
local, 25
slice
capacity, 15
length, 15
string literal
interpreted, 6
raw, 6
variables
C Bibliography
[2] et al. Arends. Protocol modifications for the dns security extensions. http://www.
ietf.org/rfc/rfc4035.txt, 2005.
[3] et al. Arends. Resource records for the dns security extensions. http://www.ietf.
org/rfc/rfc4034.txt, 2005.
[15] Perl Package Authors. Comprehensive perl archive network. http://cpan.org/, 2010.
[19] Go Community. Go issue 65: Compiler can’t spot guaranteed return in if statement.
http://code.google.com/p/go/issues/detail?id=65, 2010.
[23] Brian Kernighan Dennis Ritchie. The c programming language. ..., 2010.
[32] Rob Pike. Newsqueak: A language for communicating with mice. http://swtch.com/
~rsc/thread/newsqueak.pdf, 2010.