Golang println function
last modified May 8, 2025
This tutorial explains how to use the println built-in function in Go.
We'll cover basic output with practical examples of debugging and logging.
The println function is a built-in function in Go that writes to
standard error. It's primarily used for debugging and temporary output during
development. Unlike fmt.Println, it doesn't require imports.
In Go, println accepts multiple arguments of any type and prints
space-separated values. It adds a newline at the end. The output format is
implementation-dependent and may vary across platforms.
Basic println example
The simplest use of println outputs values to standard error.
This example demonstrates basic printing of different value types.
Note: For production code, prefer fmt package.
package main
func main() {
println("Hello, Go!")
println(42)
println(3.14)
println(true)
println('A')
}
The example prints a string, integer, float, boolean, and rune. Each call
to println outputs on a new line. The output format is basic.
Printing multiple values
println can accept multiple arguments of different types.
This example shows how to print several values in one call.
package main
func main() {
name := "Alice"
age := 30
height := 1.75
println("Name:", name, "Age:", age, "Height:", height)
a, b := 10, 20
println("Values:", a, b, "Sum:", a+b)
}
The function prints all arguments space-separated. Expressions are evaluated
before printing. The output format is less customizable than fmt.
Debugging with println
println is often used for quick debugging during development.
This example demonstrates its use for variable inspection.
package main
func calculate(x, y int) int {
println("calculate called with", x, y)
result := x * y
println("intermediate result:", result)
return result + 1
}
func main() {
a := 5
b := 7
println("Before calculation")
res := calculate(a, b)
println("Final result:", res)
}
The debug prints help track function calls and intermediate values. This is useful for quick diagnostics without proper logging setup.
Println vs fmt.Println
This example compares println with fmt.Println.
It highlights formatting differences between the two approaches.
package main
import "fmt"
func main() {
value := 3.141592653589793
println("Built-in println:", value)
fmt.Println("fmt.Println:", value)
println("Built-in (struct):", struct{ x int }{42})
fmt.Println("fmt.Println (struct):", struct{ x int }{42})
}
fmt.Println provides better formatting for complex types.
Built-in println output is more basic and less consistent.
Println in goroutines
println can be safely used from multiple goroutines.
This example demonstrates concurrent printing.
package main
import (
"sync"
"time"
)
func worker(id int, wg *sync.WaitGroup) {
defer wg.Done()
println("Worker", id, "started")
time.Sleep(time.Second)
println("Worker", id, "finished")
}
func main() {
var wg sync.WaitGroup
for i := 1; i <= 3; i++ {
wg.Add(1)
go worker(i, &wg)
}
wg.Wait()
println("All workers completed")
}
The output shows interleaved messages from different goroutines.
println handles concurrent calls safely, though output
may be mixed unpredictably.
Source
This tutorial covered the println function in Go with practical
examples of debugging and temporary output scenarios.
Author
List all Golang tutorials.