ZetCode

Golang print function

last modified May 8, 2025

This tutorial explains how to use the print built-in function in Go. We'll cover basic output with practical examples of debugging and logging.

The print function is a built-in function in Go that writes to standard error. It's primarily used for debugging and simple output needs. Unlike fmt.Print, it doesn't require importing any packages.

In Go, print is convenient for quick debugging but has limitations. It doesn't support formatting and always writes to stderr. For production code, fmt package functions are generally preferred.

Basic print example

The simplest use of print outputs a string to standard error. This example demonstrates basic string output.
Note: The output appears on stderr, not stdout.

basic_print.go
package main

func main() {
    print("Hello, World!\n")
    print("This is a test message\n")
}

The code prints two lines to standard error. The newline character \n is used to create line breaks. This is the simplest form of debugging output.

Printing multiple values

print can accept multiple arguments of different types. This example shows printing various data types together.

multiple_values.go
package main

func main() {
    name := "Alice"
    age := 30
    height := 5.8
    
    print("Name:", name, " Age:", age, " Height:", height, "\n")
}

The function automatically converts all arguments to strings and concatenates them. Note there are no spaces between values unless explicitly added.

Printing boolean values

print can display boolean values directly. This example shows printing the results of boolean expressions.

boolean_print.go
package main

func main() {
    a := true
    b := false
    
    print("a is", a, "and b is", b, "\n")
    print("a AND b is", a && b, "\n")
    print("a OR b is", a || b, "\n")
}

Boolean values are printed as true or false. The output shows both variable values and expression results.

Printing during program execution

print is useful for tracing program flow. This example shows printing at different execution points.

execution_trace.go
package main

func calculate(x int) int {
    print("Entering calculate with x =", x, "\n")
    result := x * 2
    print("Exiting calculate with result =", result, "\n")
    return result
}

func main() {
    print("Program started\n")
    res := calculate(5)
    print("Final result:", res, "\n")
    print("Program completed\n")
}

The output shows the program's execution path. This technique helps debug function calls and track variable changes.

Printing pointer values

print can display memory addresses when given pointers. This example demonstrates printing pointer information.

pointer_print.go
package main

func main() {
    x := 42
    ptr := &x
    
    print("Value of x:", x, "\n")
    print("Address of x:", ptr, "\n")
    print("Value through pointer:", *ptr, "\n")
}

The output shows both the value and memory address. Pointer values are printed in hexadecimal format by default.

Source

Go language specification

This tutorial covered the print function in Go with practical examples of basic output and debugging techniques.

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.