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

About printing output

As with UNIX and C, Go offers a variety of ways for printing your output on the screen. All the printing functions of this section require the use of the fmt Go standard package and are illustrated in the printing.go program, which will be presented in two parts.

The simplest way to print something in Go is by using the fmt.Println() and the fmt.Printf() functions. The fmt.Printf() function has many similarities with the C printf(3) function. You can also use the fmt.Print() function instead of fmt.Println(). The main difference between fmt.Print() and fmt.Println() is that the latter automatically adds a newline character each time you call it.

On the other hand, the biggest difference between fmt.Println() and fmt.Printf() is that the latter requires a format specifier for each thing that you want to print, just like the C printf(3) function, which means that you have better control of what you are doing, but you have to write more code. Go calls these format specifiers verbs. You can find more information about verbs at

If you have to perform any formatting before printing something or have to arrange multiple variables, then using fmt.Printf() might be a better choice. However, if you only have to print a single variable, then you might need to choose either fmt.Print() or fmt.Println(), depending on whether you need a newline character or not.

The first part of printing.go contains the following Go code:

package main 
import ( 
func main() { 
    v1 := "123" 
    v2 := 123 
    v3 := "Have a nice day\n" 
    v4 := "abc" 

In this part, you see the import of the fmt package and the definition of four Go variables. The \n used in v3 is the line break character. However, if you just want to insert a line break in your output, you can call fmt.Println() without any arguments, instead of using something like fmt.Print("\n").

The second part is as follows:

    fmt.Print(v1, v2, v3, v4) 
    fmt.Println(v1, v2, v3, v4) 
    fmt.Print(v1, " ", v2, " ", v3, " ", v4, "\n") 
    fmt.Printf("%s%d %s %s\n", v1, v2, v3, v4) 

In this part, you print the four variables using fmt.Println(), fmt.Print(), and fmt.Printf() in order to better understand how they differ.

If you execute printing.go, you will get the following output:

$ go run printing.go
123123Have a nice day
123 123 Have a nice day
123 123 Have a nice day
123123 Have a nice day

As you can see from the preceding output, the fmt.Println() function also adds a space character between its parameters, which is not the case with fmt.Print().

As a result, a statement such as  fmt.Println(v1, v2) is equivalent to fmt.Print(v1, " ", v2, "\n").

Apart from fmt.Println(), fmt.Print(), and fmt.Printf(), which are the simplest functions that can be used for generating output on the screen, there is also the S family of functions that includes fmt.Sprintln(), fmt.Sprint(), and fmt.Sprintf(). These functions are used to create strings based on a given format.

Finally, there is the F family of functions, which includes fmt.Fprintln(), fmt.Fprint(), and fmt.Fprintf(). They are used for writing to files using an io.Writer.

You will learn more about the io.Writer and io.Reader interfaces in Chapter 8, Telling a UNIX System What to Do.

The next section will teach you how to print your data using standard output, which is pretty common in the UNIX world.