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 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 https://golang.org/pkg/fmt/.

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 ( 
    "fmt" 
) 
 
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() 
    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
abc
123 123 Have a nice day
abc
123 123 Have a nice day
abc
123123 Have a nice day
abc
  

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.

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