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 error output

This section will present a technique for sending data to UNIX standard error, which is the UNIX way of differentiating between actual values and error output.

The Go code for illustrating the use of standard error in Go is included in stdERR.go and will be presented in two parts. As writing to standard error requires the use of the file descriptor related to standard error, the Go code of stdERR.go will be based on the Go code of stdOUT.go.

The first part of the program is as follows:

package main 
import ( 
func main() { 
    myString := "" 
    arguments := os.Args 
    if len(arguments) == 1 { 
        myString = "Please give me one argument!" 
    } else { 
        myString = arguments[1] 

So far, stdERR.go is almost identical to stdOUT.go.

The second portion of stdERR.go is the following:

    io.WriteString(os.Stdout, "This is Standard output\n") 
    io.WriteString(os.Stderr, myString) 
    io.WriteString(os.Stderr, "\n") 

You call io.WriteString() two times to write to standard error (os.Stderr) and one more time to write to standard output (os.Stdout).

Executing stdERR.go will create the following output:

$ go run stdERR.go
This is Standard output
Please give me one argument!

The preceding output cannot help you to differentiate between data written to standard output and data written to standard error, which can be very useful sometimes. However, if you are using the bash(1) shell, there is a trick you can use in order to distinguish between standard output data and standard error data. Almost all UNIX shells offer this functionality in their own way.

When using bash(1), you can redirect the standard error output to a file as follows:

$ go run stdERR.go 2>/tmp/stdError
This is Standard output
$ cat /tmp/stdError
Please give me one argument!
The number after the name of a UNIX program or system call refers to the section of the manual its page belongs to. Although most of the names can be found only once in the manual pages, which means that putting the section number is not required, there are names that can be located in multiple sections because they have multiple meanings, such as crontab(1) and crontab(5). Therefore, if you try to retrieve the manual page of a name with multiple meanings without stating its section number, you will get the entry that has the smallest section number.

Similarly, you can discard error output by redirecting it to the /dev/null device, which is like telling UNIX to completely ignore it:

$ go run stdERR.go 2>/dev/null
This is Standard output

In the two examples, we redirected the file descriptor of standard error into a file and /dev/null, respectively. If you want to save both standard output and standard error to the same file, you can redirect the file descriptor of standard error (2) to the file descriptor of standard output (1). The following command shows the technique, which is pretty common in UNIX systems:

$ go run stdERR.go >/tmp/output 2>&1
$ cat /tmp/output
This is Standard output
Please give me one argument!

Finally, you can send both standard output and standard error to /dev/null as follows:

$ go run stdERR.go >/dev/null 2>&1