ZetCode

Golang fmt.Println function

last modified May 8, 2025

This tutorial explains how to use the fmt.Println function in Go. We'll cover basic output with practical examples of formatted printing.

The fmt.Println function is used to print values to standard output in Go. It automatically adds spaces between values and a newline at the end. This is one of the most commonly used functions for console output.

In Go, fmt.Println is part of the fmt package which implements formatted I/O. It can print any number of values of any type with simple formatting.

Basic fmt.Println example

The simplest use of fmt.Println prints a single value to the console. This example demonstrates basic output functionality.
Note: The function adds a newline automatically.

basic_println.go
package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}

This code prints the string "Hello, Go!" to standard output followed by a newline. The fmt package must be imported to use Println.

Printing multiple values

fmt.Println can accept multiple arguments of different types. This example shows printing several values with automatic spacing.

multiple_values.go
package main

import "fmt"

func main() {
    name := "Alice"
    age := 30
    fmt.Println("Name:", name, "Age:", age)
}

The function automatically inserts spaces between values and adds a newline. Different types (string and int) are converted to strings automatically.

Printing variables and literals

fmt.Println can mix variables and literals in its arguments. This example demonstrates combining different value types in one call.

variables_literals.go
package main

import "fmt"

func main() {
    pi := 3.14159
    fmt.Println("The value of pi is approximately", pi, "and", 22/7, "is a rough approximation")
}

The output combines a string literal, float variable, and integer expression. All values are converted to strings and separated by spaces automatically.

Printing special characters

fmt.Println handles special characters like newlines and tabs. This example shows how escape sequences are processed in the output.

special_chars.go
package main

import "fmt"

func main() {
    fmt.Println("First line\nSecond line")
    fmt.Println("Column1\tColumn2\tColumn3")
    fmt.Println("Backslash: \\")
}

Escape sequences like \n, \t, and \\ are interpreted as special characters. The function processes these before sending the output to the console.

Printing structs and slices

fmt.Println can print complex data structures like structs and slices. This example demonstrates automatic formatting of composite types.

composite_types.go
package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{"Bob", 42}
    nums := []int{1, 2, 3, 4, 5}
    
    fmt.Println("Person:", p)
    fmt.Println("Numbers:", nums)
}

Structs and slices are automatically converted to human-readable strings. The output shows the internal representation of these composite types.

Printing to multiple lines

Multiple calls to fmt.Println create separate lines of output. This example shows how consecutive calls produce distinct lines.

multi_line.go
package main

import "fmt"

func main() {
    fmt.Println("Line 1")
    fmt.Println("Line 2")
    fmt.Println("Line 3")
    
    // Equivalent to:
    fmt.Print("Line A\n")
    fmt.Print("Line B\n")
    fmt.Print("Line C\n")
}

Each fmt.Println call ends with a newline, creating separate lines. The example also shows the equivalent using fmt.Print with \n.

Printing boolean values

fmt.Println correctly formats boolean values as "true" or "false". This example demonstrates printing boolean expressions and variables.

booleans.go
package main

import "fmt"

func main() {
    a := true
    b := false
    fmt.Println("a is", a, "and b is", b)
    fmt.Println("5 > 3 is", 5 > 3)
    fmt.Println("Logical AND:", a && b)
}

Boolean values are converted to their string representations automatically. The output shows both boolean variables and expressions evaluated to booleans.

Source

Go fmt package documentation

This tutorial covered the fmt.Println function in Go with practical examples of formatted output to the console.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.