-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Mastering Go
By :

Although Go is a general-purpose programming language, it is primarily used for writing system tools, command line utilities, web services, and software that works over networks and the internet. You can use Go for teaching programming, and it is a good candidate as your first programming language because of its lack of verbosity and clear ideas and principles.
Go can help you develop the following kinds of applications:
docker
and hugo
Although Go is a very practical and competent programming language, it is not perfect:
fork(2)
to serve its clients—Go does not support the functionality of fork(2)
. However, in most cases, designing your application with goroutines and channels in mind will solve your problems.There are many things that Go does better than other programming languages, including the following:
The next subsection describes my personal Go journey.
In this subsection, I am going to tell you my personal story of how I ended up learning and using Go. I am a UNIX person, which means that I like UNIX and prefer to use it whenever possible. I also love C, and I used to like C++; I wrote a command line FTP client in C++ for my M.Sc. project. Nowadays, C++ is just a huge programming language that is difficult to learn. Although C continues to be a decent programming language, it requires lots of code to perform simple tasks and suffers from difficult-to-find and correct bugs, due to manual memory management and extremely flexible conversion between different data types without any warnings or error messages.
As a result, I used to use Perl to write simple command line utilities. However, Perl is far from perfect for writing serious command line tools and services, as it is a scripting programming language and is not intended for web development.
When I first heard about Go, that it was developed by Google, and that both Rob Pike and Ken Thomson were involved in its development, I instantly became interested in Go.
Since then, I have used Go to create web services, servers, and clients that communicate with RabbitMQ, MySQL, and PostgreSQL, create simple command line utilities, implement algorithms for time series data mining, create utilities that generate synthetic data, etc.
Soon, we are going to move on to actually learn some Go, using Hello World! as the first example, but before that, we will present the go doc
command, which allows you to find information about the Go standard library, its packages, and their functions, as well as the godoc
utility.
If you have not already installed Go, this is the right time to do so. To do that, visit https://go.dev/dl/ or use your favorite package manager.
The Go distribution comes with a plethora of tools that can make your life as a programmer easier. Two of these tools are the go doc
subcommand and godoc
utility, which allow you to see the documentation of existing Go functions and packages without needing an internet connection. However, if you prefer viewing the Go documentation online, you can visit https://pkg.go.dev/.
The go doc
command can be executed as a normal command line application that displays its output on a terminal, and it is similar to the UNIX man(1)
command, but for Go functions and packages only. So, in order to find out information about the Printf()
function of the fmt
package, you should execute the following command:
$ go doc fmt.Printf
Similarly, you can find out information about the entire fmt
package by running the following command:
$ go doc fmt
As godoc
is not installed by default, you might need to install it by running go install golang.org/x/tools/cmd/godoc@latest
. The godoc
binary is going to be installed in ~/go/bin
, and you can execute it as ~/go/bin/godoc
unless ~/go/bin
is in your PATH
environment variable.
The godoc
command line application starts a local web server. So you need a web browser to look at the Go documentation.
Running godoc
requires executing godoc
with the -http
parameter:
$ ~/go/bin/godoc -http=:8001
The numeric value in the preceding command, which in this case is 8001
, is the port number that the HTTP server will listen to. As we have omitted the IP address, godoc
is going to listen to all network interfaces.
You can choose any port number that is available if you have the right privileges. However, note that port numbers 0
–1023
are restricted and can only be used by the root user, so it is better to avoid choosing one of those and pick something else, if it is not already in use by a different process. Port number 8001
is usually free and is frequently used for local HTTP servers.
You can omit the equals sign in the presented command and put a space character in its place. So the following command is completely equivalent to the previous one:
$ ~/go/bin/godoc -http :8001
After that, you should point your web browser to http://localhost:8001/
in order to get the list of available Go packages and browse their documentation. If no -http
parameter is provided, godoc
listens to port 6060
.
If you are using Go for the first time, you will find the Go documentation very handy for learning the parameters and the return values of the functions you want to use — as you progress in your Go journey, you will use the Go documentation to learn the gory details of the functions and variables that you want to use.
The next section presents the first Go program of the book and explains the basic concepts of Go.