Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Go

You're reading from   Mastering Go Create Golang production applications using network libraries, concurrency, machine learning, and advanced data structures

Arrow left icon
Product type Paperback
Published in Aug 2019
Publisher Packt
ISBN-13 9781838559335
Length 798 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Mihalis Tsoukalos Mihalis Tsoukalos
Author Profile Icon Mihalis Tsoukalos
Mihalis Tsoukalos
Arrow right icon
View More author details
Toc

Table of Contents (15) Chapters Close

1. Go and the Operating System 2. Understanding Go Internals FREE CHAPTER 3. Working with Basic Go Data Types 4. The Uses of Composite Types 5. How to Enhance Go Code with Data Structures 6. What You Might Not Know About Go Packages and Functions 7. Reflection and Interfaces for All Seasons 8. Telling a UNIX System What to Do 9. Concurrency in Go – Goroutines, Channels, and Pipelines 10. Concurrency in Go – Advanced Topics 11. Code Testing, Optimization, and Profiling 12. The Foundations of Network Programming in Go 13. Network Programming – Building Your Own Servers and Clients 14. Machine Learning in Go 15. Other Books You May Enjoy

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 ( 
    "io" 
    "os" 
) 
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
  
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime