Table of Contents
Introduction vii
1 Getting Started 1
Machine Setup 1
Text Editors 2
The Terminal 2
Environment 2
Go 3
Your First Program 4
How to Read a Go Program 5
Exercises 8
2 Types 9
Numbers 10
Integers 10
Floating-Point Numbers 10
Example 11
Strings 12
Booleans 13
Exercises 15
3 Variables 17
How to Name a Variable 20
Scope 20
Constants 21
Defining Multiple Variables 22
An Example Program 22
Exercises 23
4 Control Structures 25
The for Statement 26
The if Statement 27
The switch Statement 29
Exercises 31
5 Arrays, Slice, and Maps 33
Arrays 33
Slices 36
Append 37
Copy 37
Maps 38
Exercises 42
6 Functions 43
Your Second Function 43
Variadic Functions 47
Closure 47
Recursion 48
Defer, panic, and recover 49
Panic and recover 50
Pointers 51
The * and & operators 51
New 52
Exercises 53
7 Structs and Interfaces 55
Structs 56
Initialization 56
Fields 56
Methods 57
Embedded Types 58
Interfaces 59
Exercises 62
8 Packages 63
The Core Packages 63
Strings 64
Input/Output 67
Files and Folders 68
Errors 70
Containers and Sort 70
Hashes and Cryptography 73
Servers 75
TCP 75
HTTP 77
RPC 78
Parsing Command-Line Arguments 79
Creating Packages 79
Documentation 81
Exercises 82
9 Testing 83
Exercises 86
10 Concurrency 87
Goroutines 87
Channels 89
Channel Direction 91
Select 91
Buffered Channels 92
An Example 93
Exercises 96
11 Next Steps 97
Study the Masters 97
Make Something 98
Team Up 98
A Answers 99
Index 109