ZetCode

Golang fmt.Print function

last modified May 8, 2025

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

The fmt.Print function writes to standard output without formatting. It prints its arguments in the order they appear, with spaces between them. Unlike fmt.Println, it doesn't add a newline at the end.

In Go, fmt.Print is part of the fmt package for I/O operations. It's useful for simple output when you don't need special formatting or newline control.

Basic fmt.Print example

The simplest use of fmt.Print outputs values to the console. This example demonstrates basic printing of different value types.
Note: Values are printed with spaces between them.

basic_print.go
package main

import "fmt"

func main() {
    fmt.Print("Hello, ")
    fmt.Print("World!")
    fmt.Print(42)
    fmt.Print(true)
    fmt.Print("\n") // Manual newline
}

The output appears on one line: "Hello, World!42true". Notice how values are concatenated without automatic spacing or newlines.

Printing multiple values

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

multiple_values.go
package main

import "fmt"

func main() {
    name := "Alice"
    age := 30
    pi := 3.14159
    
    fmt.Print("Name: ", name, " Age: ", age, " Pi: ", pi, "\n")
    fmt.Print("Types - name: ", fmt.Sprintf("%T", name), 
              " age: ", fmt.Sprintf("%T", age), "\n")
}

The function automatically converts values to strings and separates them with spaces. The output shows all values on one line.

Printing without spaces

To print values without automatic spacing, concatenate them first. This example demonstrates controlling spacing in output.

no_spaces.go
package main

import "fmt"

func main() {
    fmt.Print("The answer is" + " " + fmt.Sprint(42) + "\n")
    fmt.Print("No", "Spaces", "Between", "Words", "\n")
    fmt.Print("Concatenated" + "String" + "\n")
}

String concatenation with + operator avoids automatic spacing. The output shows tightly packed text without extra spaces.

Printing special characters

fmt.Print handles special characters like tabs and newlines. This example shows using escape sequences in output.

special_chars.go
package main

import "fmt"

func main() {
    fmt.Print("First line\nSecond line\n")
    fmt.Print("Column1\tColumn2\tColumn3\n")
    fmt.Print("Backslash: \\ \t Quote: \" \t Apostrophe: '\n")
    fmt.Print("Alert: \a \t Backspace: \b \t Form feed: \f\n")
}

Escape sequences work as expected, providing control over output format. Note that some special characters may behave differently across terminals.

Printing structs and slices

fmt.Print can print complex types like structs and slices. This example demonstrates printing composite data types.

composite_types.go
package main

import "fmt"

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{"Bob", 25}
    nums := []int{1, 2, 3, 4, 5}
    
    fmt.Print("Person: ", p, "\n")
    fmt.Print("Numbers: ", nums, "\n")
    fmt.Print("Slice capacity: ", cap(nums), "\n")
}

Composite types are printed in a default format. For custom formatting, consider using fmt.Printf with format specifiers.

Printing to a file

fmt.Fprint is the generalized version that writes to any io.Writer. This example shows printing to a file instead of standard output.

file_print.go
package main

import (
    "fmt"
    "os"
)

func main() {
    file, err := os.Create("output.txt")
    if err != nil {
        panic(err)
    }
    defer file.Close()
    
    fmt.Fprint(file, "This goes to a file\n")
    fmt.Fprint(file, "Multiple ", "values ", 42, "\n")
    
    fmt.Print("Check output.txt for results\n")
}

The fmt.Fprint function works like fmt.Print but writes to the specified io.Writer. This pattern works for any writer.

Performance considerations

For high-performance printing, buffer your output before writing. This example compares different printing approaches.

performance.go
package main

import (
    "fmt"
    "strings"
    "time"
)

func printMethod() {
    start := time.Now()
    for i := 0; i < 10000; i++ {
        fmt.Print("iteration ", i, " ")
    }
    fmt.Println("\nfmt.Print duration:", time.Since(start))
}

func builderMethod() {
    start := time.Now()
    var b strings.Builder
    for i := 0; i < 10000; i++ {
        fmt.Fprint(&b, "iteration ", i, " ")
    }
    fmt.Print(b.String())
    fmt.Println("\nstrings.Builder duration:", time.Since(start))
}

func main() {
    printMethod()
    builderMethod()
}

Buffering output with strings.Builder is faster for many small writes. The difference becomes significant in performance-critical code.

Source

Go fmt package documentation

This tutorial covered the fmt.Print function in Go with practical examples of basic output operations and formatting.

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.