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

Two Go rules

Go has strict coding rules that are there to help you avoid silly errors and bugs in your code, as well as to make your code easier to read for the Go community. This section will present two such Go rules that you need to know.

As mentioned earlier, please remember that the Go compiler is there to help and not make your life miserable. As a result, the main purpose of the Go compiler is to compile and increase the quality of your Go code.

You either use a Go package or you do not include it

Go has strict rules about package usage. Therefore, you cannot just include any package you might think that you will need and then not use it afterward.

Look at the following naive program, which is saved as packageNotUsed.go:

package main 
 
import ( 
    "fmt" 
    "os" 
) 
 
func main() { 
    fmt.Println("Hello there!") 
} 
In this book, you are going to see lots of error messages, error situations, and warnings. I believe that looking at code that fails to compile is also useful and sometimes even more valuable than just looking at Go code that compiles without any errors. The Go compiler usually displays useful error messages and warnings that will most likely help you to resolve an erroneous situation, so do not underestimate Go error messages and warnings.

If you execute packageNotUsed.go, you will get the following error message from Go and the program will not get executed:

$ go run packageNotUsed.go
# command-line-arguments
./packageNotUsed.go:5:2: imported and not used: "os"
  

If you remove the os package from the import list of the program, packageNotUsed.go will compile just fine; try it on your own.

Although this is not the perfect time to start talking about breaking Go rules, there is a way to bypass this restriction. This is showcased in the following Go code that is saved in the packageNotUsedUnderscore.go file:

package main 
 
import ( 
    "fmt" 
    _ "os" 
) 
 
func main() { 
    fmt.Println("Hello there!") 
} 

So, using an underscore character in front of a package name in the import list will not create an error message in the compilation process even if that package will not be used in the program:

$ go run packageNotUsedUnderscore.go
Hello there!
  
The reason that Go is allowing you to bypass this rule will become more evident in Chapter 6, What You Might Not Know About Go Packages and Go Functions.

There is only one way to format curly braces

Look at the following Go program named curly.go:

package main 
 
import ( 
    "fmt" 
) 
 
func main() 
{ 
    fmt.Println("Go has strict rules for curly braces!") 
} 

Although it looks just fine, if you try to execute it, you will be fairly disappointed, because you will get the following syntax error message and the code will not compile and therefore run:

$ go run curly.go
# command-line-arguments
./curly.go:7:6: missing function body for "main"
./curly.go:8:1: syntax error: unexpected semicolon or newline before {
  

The official explanation for this error message is that Go requires the use of semicolons as statement terminators in many contexts, and the compiler automatically inserts the required semicolons when it thinks that they are necessary. Therefore, putting the opening curly brace ({) in its own line will make the Go compiler insert a semicolon at the end of the previous line (func main()), which is the cause of the error message.