The Getting Started chapter will help you set up your Go development environment, and it lays down some conventions for the rest of the book.
Chapter 1 and Chapter 2 discuss building command-line applications. You will learn to use the standard library packages to develop scalable and testable command-line programs.
Chapter 3 and Chapter 4 teach you how to build production-ready HTTP clients. You will learn to configure time-outs, understand connection pooling behavior, implement middleware components, and more.
Chapters 5 through 7 discuss building HTTP server applications. You will learn how to add support for streaming data, implement middleware components, share data across handler functions, and implement various techniques to improve the robustness of your applications.
Chapters 8 through 10 delve deep into building RPC applications using gRPC. You will learn about Protocol Buffers, implement various RPC communication patterns, and implement client-side and server-side interceptors to perform common application functionality.
In Chapter 11, you will learn to interact with object stores and relational database management systems from your applications.
Appendix A briefly discusses how you can add instrumentation into your applications.
Appendix B provides some guidelines around deploying your applications.
Each group of chapters is mostly independent from the other groups. So feel free to jump to the first chapter of a group; however, there may be references to a previous chapter.
Within each group, however, I recommend reading the chapters from beginning to end, as the chapters within a group build upon the previous chapter. For example, if you are keen to learn more about writing HTTP clients, I suggest reading Chapter 3 and Chapter 4 in that order.
I also encourage you to write and run the code yourself as you work through the book and to attempt the exercises as well. Writing the programs yourself in your code editor will build that Go muscle, as it certainly did for me while writing the programs in the book.
Reader Support for This Book
You can find links to the source code and resources related to the book at https://practicalgobook.net
. The code from the book is also posted at https://www.wiley.com/go/practicalgo
.
If you believe that you've found a mistake in this book, please bring it to our attention. At John Wiley & Sons, we understand how important it is to provide our customers with accurate content, but even with our best efforts an error may occur. To submit your possible errata, please email it to our Customer Service Team at [email protected]
with the subject line “Possible Book Errata Submission.”
Getting Started
To start off, we will install the necessary software needed for the rest of the book. We will also go over some of the conventions and assumptions made throughout. Finally, I will point out key language features you will use in the book and resources to refresh your knowledge about them.
Installing Go
The code listings in this book work with Go 1.16 and above. Follow the instructions at https://go.dev/learn/
to install the latest version of the Go compiler for your operating system. It usually involves downloading and running a graphical installation process for Windows or macOS. For Linux, your distribution's package repository may contain the latest version already, which means that you can use your package manager to install the Go compiler as well.
Once you have it installed, no further configuration is necessary to run the programs that you will write throughout the book. Verify that you have everything set up correctly by running the command go version
from your terminal program. You should see an output telling you which Go version is installed and the operating system and architecture. For example, on my MacBook Air (M1), I see the following:
$ go version go version go1.16.4 darwin/arm64
If you can see an output like the above, you are ready to continue with the next steps.
Choosing an Editor
If you don't yet have a favorite Go editor/integrated development environment (IDE), I recommend Visual Studio Code (https://code.visualstudio.com/download
). If you are a Vim user, I recommend the vim-go extension (https://github.com/fatih/vim-go
).
Installing Protocol Buffer Toolchain
For some chapters in the book, you will need the Protocol Buffers (protobuf) and gRPC tools for Go installed. You will install three separate programs: the protobuf compiler, protoc
, and the Go protobuf and gRPC plug-ins, protoc-gen-go
and protoc-gen-go-grpc
, respectively.
Linux and macOS
To install the compiler, run the following steps for Linux or macOS:
1 Download the latest release (3.16.0 at the time of this book's writing) file from https://github.com/protocolbuffers/protobuf/releases, corresponding to your operating system and architecture. Look for the files in the Assets section. For example, for Linux on a x86_64 system, download the file named protoc-3.16.0-linux-x86_64.zip. For macOS, download the file named protoc-3.16.3-osx-x86_64.zip .
2 Next, extract the file contents and copy them to your $HOME/.local directory using the unzip command: $ unzip protoc-3.16.3-linux-x86_64.zip -d $HOME/.local.
3 Finally, add the $HOME/.local/bin directory to your $PATH environment variable: $ export PATH="$PATH:$HOME/.local/bin" in your shell's initialization script, such as $HOME/.bashrc for Bash shell and .zshrc for Z shell.
Once you have completed the preceding steps, open a new terminal window, and run the command protoc --version
:
$ protoc --version libprotoc 3.16.0
If you see output like the one above, you are ready to move on to the next step.
To install the protobuf plug-in for Go, protoc-gen-go
(release v1.26), run the following command from a terminal window:
$ go install google.golang.org/protobuf/cmd/[email protected]
To install the gRPC plug-in for Go, protoc-gen-go-grpc
(release v1.1) tool, run the following command:
$ go install google.golang.org/grpc/cmd/[email protected]
Then add the following to your shell's initialization file ($HOME/.bashrc or $HOME/.zshrc)
:
$ export PATH="$PATH:$(go