Book Image

Mastering Go - Second Edition

By : Mihalis Tsoukalos
Book Image

Mastering Go - Second Edition

By: Mihalis Tsoukalos

Overview of this book

Often referred to (incorrectly) as Golang, Go is the high-performance systems language of the future. Mastering Go, Second Edition helps you become a productive expert Go programmer, building and improving on the groundbreaking first edition. Mastering Go, Second Edition shows how to put Go to work on real production systems. For programmers who already know the Go language basics, this book provides examples, patterns, and clear explanations to help you deeply understand Go’s capabilities and apply them in your programming work. The book covers the nuances of Go, with in-depth guides on types and structures, packages, concurrency, network programming, compiler design, optimization, and more. Each chapter ends with exercises and resources to fully embed your new knowledge. This second edition includes a completely new chapter on machine learning in Go, guiding you from the foundation statistics techniques through simple regression and clustering to classification, neural networks, and anomaly detection. Other chapters are expanded to cover using Go with Docker and Kubernetes, Git, WebAssembly, JSON, and more. If you take the Go programming language seriously, the second edition of this book is an essential guide on expert techniques.
Table of Contents (20 chapters)
Title Page

What this book covers

Chapter 1, Go and the Operating System, begins by talking about the history of Go and the advantages of Go before describing the godoc utility and explaining how you can compile and execute Go programs. After that, it talks about printing output and getting user input, working with the command-line arguments of a program and using log files. The final topic in the first chapter is error handling, which plays a key role in Go.

Chapter 2, Understanding Go Internals, discusses the Go garbage collector and the way it operates. Then, it talks about unsafe code and the unsafe package, how to call C code from a Go program, and how to call Go code from a C program.

After that, it showcases the use of the defer keyword and presents the strace(1) and dtrace(1) utilities. In the remaining sections of the chapter, you will learn how to find information about your Go environment, the use of the Go assembler, and how to generate WebAssembly code from Go.

Chapter 3, Working with Basic Go Data Types, talks about the data types offered by Go, which includes arrays, slices, and maps, as well as Go pointers, constants, loops, and working with dates and times. You would not want to miss this chapter!

Chapter 4, The Uses of Composite Types, begins by teaching you about Go structures and the struct keyword before discussing tuples, strings, runes, byte slices, and string literals. The remainder of the chapter talks about regular expressions and pattern matching, the switch statement, the strings package, the math/big package, about developing a key-value store in Go, and about working with XML and JSON files.

Chapter 5, How to Enhance Go Code with Data Structures, is about developing your own data structures when the structures offered by Go do not fit a particular problem. This includes developing binary trees, linked lists, hash tables, stacks, and queues, and learning about their advantages. This chapter also showcases the use of the structures found in the container standard Go package, as well as how to use Go to verify Sudoku puzzles and generate random numbers.

Chapter 6, What You Might Not Know About Go Packages and Functions, is all about packages and functions, which also includes the use of the init() function, the syscall standard Go package, and the text/template and html/template packages. Additionally, it shows the use of the go/scanner, go/parser, and go/token advanced packages. This chapter will definitely make you a better Go developer!

Chapter 7, Reflection and Interfaces for All Seasons, discusses three advanced Go concepts: reflection, interfaces, and type methods. Additionally, it discusses the object-oriented capabilities of Go and how to debug Go programs using Delve!

Chapter 8, Telling a UNIX System What to Do, is about Systems Programming in Go, which includes subjects such as the flag package for working with command-line arguments, handling UNIX signals, file input and output, the bytes package, the io.Reader and io.Writer interfaces, and the use of the Viper and Cobra Go packages. As I told you before, if you are really into systems programming in Go, then getting Go Systems Programming after reading Mastering Go, Second Edition, is highly recommended!

Chapter 9, Concurrency in Go – Goroutines, Channels, and Pipelines, discusses goroutines, channels, and pipelines, which is the Go way of achieving concurrency.

You will also learn about the differences between processes, threads, and goroutines, the sync package, and the way the Go scheduler operates.

Chapter 10, Concurrency in Go – Advanced Topics, will continue from the point where the previous chapter left off and make you a master of goroutines and channels! You will learn more about the Go scheduler, the use of the powerful select keyword and the various types of Go channels, as well as shared memory, mutexes, the sync.Mutex type, and the sync.RWMutex type. The final part of the chapter talks about the context package, worker pools, and how to detect race conditions.

Chapter 11, Code Testing, Optimization, and Profiling, discusses code testing, code optimization and code profiling, as well as cross-compilation, creating documentation, benchmarking Go code, creating example functions, and finding unreachable Go code.

Chapter 12, The Foundations of Network Programming in Go, is all about the net/http package and how you can develop web clients and web servers in Go. This also includes the use of the http.Response, http.Request, and http.Transport structures, and the http.NewServeMux type. You will even learn how to develop an entire web site in Go! Furthermore, in this chapter, you will learn how to read the configuration of your network interfaces and how to perform DNS lookups in Go. Additionally, you will learn how to use gRPC with Go.

Chapter 13, Network Programming – Building Your Own Servers and Clients, talks about working with HTTPS traffic, and creating UDP and TCP servers and clients in Go using the functionality offered by the net package. Other topics included in this chapter are creating RPC clients and servers as well as developing a concurrent TCP server in Go and reading raw network packages!

Chapter 14, Machine Learning in Go, talks about machine learning in Go, including classification, clustering, anomaly detection, outliers, neural networks and TensorFlow, as well as working with Apache Kafka with Go.

This book can be divided into three logical parts. The first part takes a sophisticated look at some important Go concepts, including user input and output, downloading external Go packages, compiling Go code, calling C code from Go, and creating WebAssembly from Go, as well as using Go basic types and Go composite types.

The second part starts with Chapter 5, How to Enhance Go Code with Data Structures, and also includes Chapter 6, What You Might Not Know About Go Packages and Go Functions, and Chapter 7, Reflection and Interfaces for All Seasons. These three chapters deal with Go code organization in packages and modules, the design of Go projects, and some advanced features of Go, respectively.

The last part includes the remaining seven chapters and deals with more practical Go topics. Chapters 8, 9, 10, and 11 talk about systems programming in Go, concurrency in Go, code testing, optimization, and profiling. The last three chapters of this book will talk about network programming and machine learning in Go.

The book includes content such as Go and WebAssembly, using Docker with Go, creating professional command-line tools with the Viper and Cobra packages, parsing JSON and YAML records, performing operations with matrices, working with Sudoku puzzles, the go/scanner and go/token packages, working with git(1) and GitHub, the atomic package, gRPC and Go, and HTTPS.

The book will present relatively small yet complete Go programs that illustrate the presented concepts. This has two main advantages: firstly, you do not have to look at an endless code listing when trying to learn a single technique and secondly, you can use this code as a starting point when creating your own applications and utilities.

Realizing the importance of containers and Docker, this book includes various examples of Go executable files that are used from within Docker images because Docker images offer a great way to deploy server software.