ZetCode

Golang fmt.Sprint function

last modified May 8, 2025

This tutorial explains how to use the fmt.Sprint function in Go. We'll cover string formatting basics with practical examples of converting values to strings without printing.

The fmt.Sprint function formats values and returns the resulting string. Unlike fmt.Print, it doesn't output to standard output. This is useful when you need the formatted string for further processing.

In Go, fmt.Sprint accepts any number of arguments of any type. It converts them to strings using default formatting and concatenates them. The function handles all basic types automatically.

Basic fmt.Sprint example

The simplest use of fmt.Sprint converts values to strings. This example demonstrates basic string concatenation with different types.

basic_sprint.go
package main

import (
    "fmt"
)

func main() {
    name := "Alice"
    age := 30
    height := 5.8
    
    result := fmt.Sprint(name, " is ", age, " years old and ", height, " feet tall")
    fmt.Println(result)
}

The function converts all values to strings and concatenates them. The resulting string is stored in the result variable.

Formatting numbers with fmt.Sprint

fmt.Sprint handles numeric types automatically. This example shows how it converts different numeric types to strings.

numeric_sprint.go
package main

import (
    "fmt"
)

func main() {
    intVal := 42
    floatVal := 3.14159
    complexVal := complex(2, 3)
    
    result := fmt.Sprint("Integer: ", intVal, "\n",
                        "Float: ", floatVal, "\n",
                        "Complex: ", complexVal)
    fmt.Println(result)
}

Each numeric type is converted to its string representation. The newline characters format the output across multiple lines.

Combining structs with fmt.Sprint

fmt.Sprint can format struct values. This example demonstrates how it handles custom struct types.

struct_sprint.go
package main

import (
    "fmt"
)

type Person struct {
    Name string
    Age  int
}

func main() {
    p := Person{Name: "Bob", Age: 25}
    result := fmt.Sprint("Person details: ", p)
    fmt.Println(result)
}

The struct is converted using its default string representation. The output includes both field names and values.

Using fmt.Sprint with slices

Slices are automatically formatted when passed to fmt.Sprint. This example shows slice formatting behavior.

slice_sprint.go
package main

import (
    "fmt"
)

func main() {
    numbers := []int{1, 2, 3, 4, 5}
    result := fmt.Sprint("Numbers: ", numbers)
    fmt.Println(result)
    
    words := []string{"apple", "banana", "cherry"}
    result = fmt.Sprint("Fruits: ", words)
    fmt.Println(result)
}

The slice contents are formatted with square brackets and space separators. Both integer and string slices are handled the same way.

Custom Stringer interface with fmt.Sprint

Types implementing the Stringer interface can customize their output. This example demonstrates custom string formatting.

stringer_sprint.go
package main

import (
    "fmt"
)

type Point struct {
    X, Y int
}

func (p Point) String() string {
    return fmt.Sprintf("(%d,%d)", p.X, p.Y)
}

func main() {
    p := Point{X: 10, Y: 20}
    result := fmt.Sprint("Current position: ", p)
    fmt.Println(result)
}

The Point type implements the Stringer interface. fmt.Sprint uses this custom formatting when converting the value.

Error handling with fmt.Sprint

fmt.Sprint is often used to create error messages. This example shows how to build descriptive error strings.

error_sprint.go
package main

import (
    "fmt"
    "os"
)

func checkFile(filename string) error {
    if _, err := os.Stat(filename); err != nil {
        return fmt.Errorf("file error: %v - %s", err, filename)
    }
    return nil
}

func main() {
    err := checkFile("missing.txt")
    if err != nil {
        fmt.Println(fmt.Sprint("Error occurred: ", err))
    }
}

The example combines error values with descriptive text. fmt.Sprint helps build comprehensive error messages.

Performance considerations

For simple string conversions, fmt.Sprint might be less efficient than direct conversion. This example compares different approaches.

performance_sprint.go
package main

import (
    "fmt"
    "strconv"
)

func main() {
    count := 42
    
    // Using fmt.Sprint
    result1 := fmt.Sprint("Count: ", count)
    
    // Using strconv
    result2 := "Count: " + strconv.Itoa(count)
    
    fmt.Println(result1)
    fmt.Println(result2)
}

For simple cases, strconv might be faster. However, fmt.Sprint provides more flexibility with mixed types.

Source

Go fmt package documentation

This tutorial covered the fmt.Sprint function in Go with practical examples of string formatting and conversion.

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.