About printing output

As is the case with Unix and C, Go also offers a variety of ways for printing your output on the screen. All of the printing functions in 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 fmt.Printf() functions. The fmt.Printf() function has many similarities to 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, whereas 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 over what you are doing, though 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 you 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 the newline character or not.

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

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

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

The second part follows:

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

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

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 in 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 like fmt.Println(v1, v2) is equivalent to fmt.Print(v1, " ", v2, " ").

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(), which are used for creating strings based on the given format and the F family of functions. This includes fmt.Fprintln(), fmt.Fprint() and fmt.Fprintf(), which 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.20.224.107