What printing is and why you need it
A program does most of its work invisibly. It adds numbers, makes decisions and moves data around in memory, and none of that shows up on screen unless you ask. Printing is how you ask. The text goes to the console, the panel where a program's output and its error messages appear. While you are learning, printing is also the quickest way to check what a value is at a given moment.
In Go, the standard tool for this is the function Println from the fmt package. The name stands for "format", and the function's name is "print line".
Syntax and examples
Write the package name, a dot, the function name, and put the value in parentheses:
fmt.Println(13)
This prints 13 and then moves to a new line. That ending newline is what the "ln" in the name means, so several calls in a row produce several lines:
fmt.Println(1)
fmt.Println(2)
fmt.Println(3)
1
2
3
Println accepts any kind of value: numbers, text, booleans. It also accepts several values at once, separated by commas, and prints them on one line with a space between each:
fmt.Println("a", 2, true, 3.5)
a 2 true 3.5
Calling it with nothing, fmt.Println(), prints just an empty line.
Print without the new line
Its sibling fmt.Print leaves out the newline, so the next output continues on the same line:
fmt.Print("x")
fmt.Print("y\n")
xy
Here \n inside the text is an escape sequence for a newline, which is why the second call ends the line. Most of the time Println is the one to use.
Printing the result of an expression
The value in the parentheses doesn't have to be a literal. Go evaluates it first and prints the result, so you can print a calculation or, later, the contents of a variable:
fmt.Println(9 + 1)
fmt.Println("Go" + " " + "rocks")
10
Go rocks
The first line adds two numbers, and the second joins three pieces of text. This habit of printing an intermediate value is how most beginners find out why a program doesn't behave as expected.
How the code runs
A complete Go program needs a package main line, an import of fmt and a main function. In this lesson all of that is already in place, so you only write the statement:
package main
import "fmt"
func main() {
fmt.Println(1998)
}
Statements run from top to bottom, one after another, so the order of your Println calls is the order of the output lines.
Common mistakes and gotchas
- Lowercase
println. Go is case-sensitive, and only names starting with a capital letter are available from another package.fmt.Printlnworks;fmt.printlndoes not. - Forgetting the
fmt.prefix.Println(1998)alone fails, becausePrintlnlives in thefmtpackage. - Importing
fmtwithout using it. Go treats an unused import as a compile error. A program that importsfmtmust call something from it. - Expecting a number and getting text.
fmt.Println(9 + 1)prints10, butfmt.Println("9" + "1")joins two pieces of text and prints91. Quotes make a value text. - Missing parentheses.
fmt.Println 13is a syntax error. The value goes inside the parentheses.
The exercise
The task is to print the number 1998. The fmt package is already imported, so one line is enough:
fmt.Println(1998)
It prints 1998 followed by a newline. The number has no quotes: it is a number, not text, and Println writes it as its digits either way.
Key takeaways
- The console is where output and error messages appear.
fmt.Println(value)prints the value and then a newline;fmt.Printdoesn't add the newline.- Several values separated by commas go on one line with spaces between them.
- Names are case-sensitive:
Printlnneeds its capitalPand thefmt.prefix.