Start learning free

LESSON 1 OF 338 · WELCOME TO GO

Printing to the Console in Go: fmt.Println

Lesson Read the full explanation

TL ; DR:

  • The console is a panel that displays important messages, like output and errors.


  • In Go, we use fmt.Println() to print something and move to a new line. For example:

    fmt.Println(13)
    

    prints 13 to the console.



Full lesson:

Much of the work the computer does with our code is invisible by default. If we want to see a value on the screen, we can print it to the console.


fmt.Println():

In Go, fmt.Println() prints the value we give it and then prints a newline character.

fmt.Println(1)
fmt.Println(2)
fmt.Println(3)

The output is:

1
2
3

Assignment
Follow the Coding Tutorial and let's print a value to the console!


Hint
Use fmt.Println().


STEP 1 OF 1
    Ctrl + Enter to check

    GO · WELCOME TO GO

    Print in Go, explained

    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.Println works; fmt.println does not.
    • Forgetting the fmt. prefix. Println(1998) alone fails, because Println lives in the fmt package.
    • Importing fmt without using it. Go treats an unused import as a compile error. A program that imports fmt must call something from it.
    • Expecting a number and getting text. fmt.Println(9 + 1) prints 10, but fmt.Println("9" + "1") joins two pieces of text and prints 91. Quotes make a value text.
    • Missing parentheses. fmt.Println 13 is 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.Print doesn't add the newline.
    • Several values separated by commas go on one line with spaces between them.
    • Names are case-sensitive: Println needs its capital P and the fmt. prefix.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free