Learnmonkey Learnmonkey Logo

Printing In Go

What is Printing?

In the last tutorial when we made our first program, we wrote Hello, World! in the command line. Displaying text in the command line is called printing. Printing is a very important part of of programming. Printing can be used to make things such as text-based games and is also used for simple debugging and testing.

How can I print text in Go?

We already know that we can simply write fmt.Println(text) to print stuff in Go. We can change text to be any data type we want.

Usage

There are many ways you can use the Go fmt.Println function

First, you can simply put the text as a string.

For example, some examples of doing this are:

fmt.Println("Share Learnmonkey with your friends")
fmt.Println("qwerty")

We can also use fmt.Println to print a number: an integer or decimal(floating point). To do that, we can write fmt.Println(number).

For example, that could look like:

fmt.Println(36257265)
fmt.Println(973.256347)

Finally, we can place different Data Types in the function, seperated with commas like this:

fmt.Println("My", "Name", "Is", "Joe")

This will tell the fmt.Println function to print each of the strings but sperated with spaces so the end result will be: My Name Is Joe.

The fmt.Print Function

In addition to Println, Go also has a Print. The ln in Println actually stands for line, so Go makes a new line after every Println statement. The Print function doesn't make a new line. Also, when you do fmt.Print("Hello","World") you get HelloWorld instead of Hello World. You can do everything else the same.

The fmt.Printf function

The Printf() function uses a pre-defined format for the output, in which we can put variables.