Golang fmt Sprintf Function

The fmt.Sprintf function in Golang is part of the fmt package and is used to create formatted strings. Unlike fmt.Printf, which prints directly to the console, fmt.Sprintf returns the formatted string for further use, making it useful for building strings dynamically and efficiently.

Table of Contents

  1. Introduction
  2. Sprintf Function Syntax
  3. Examples
    • Basic Usage
    • Formatting Numbers and Strings
  4. Real-World Use Case
  5. Conclusion

Introduction

The fmt.Sprintf function allows you to format strings by embedding variables and expressions into a string template. This function is versatile and supports a wide range of format specifiers for different data types, enabling you to create readable and informative string outputs.

Sprintf Function Syntax

The syntax for the fmt.Sprintf function is as follows:

func Sprintf(format string, a ...interface{}) string

Parameters:

  • format: A string containing the format specifiers, which define how the subsequent arguments will be formatted.
  • a: The arguments to be formatted according to the format specifiers.

Returns:

  • A string that contains the formatted output.

Examples

Basic Usage

The following example demonstrates how to use the fmt.Sprintf function to create a formatted string.

Example

package main

import (
	"fmt"
)

func main() {
	name := "Alice"
	age := 30

	// Create a formatted string using fmt.Sprintf
	message := fmt.Sprintf("My name is %s and I am %d years old.", name, age)
	
	// Print the formatted string
	fmt.Println(message)
}

Output:

My name is Alice and I am 30 years old.

Formatting Numbers and Strings

You can use fmt.Sprintf to format numbers and strings with specific patterns, such as decimal precision or padding.

Example

package main

import (
	"fmt"
)

func main() {
	pi := 3.14159
	count := 5

	// Format the number with two decimal places
	number := fmt.Sprintf("Pi to two decimal places is %.2f", pi)
	
	// Format the string with padding
	quantity := fmt.Sprintf("Item count: %05d", count)

	// Print the formatted strings
	fmt.Println(number)
	fmt.Println(quantity)
}

Output:

Pi to two decimal places is 3.14
Item count: 00005

Real-World Use Case

Constructing SQL Queries

In real-world applications, fmt.Sprintf can be used to construct SQL queries dynamically by embedding values into query templates.

Example

package main

import (
	"fmt"
)

func main() {
	table := "users"
	condition := "age > 18"

	// Construct an SQL query using fmt.Sprintf
	query := fmt.Sprintf("SELECT * FROM %s WHERE %s;", table, condition)

	// Print the SQL query
	fmt.Println(query)
}

Output:

SELECT * FROM users WHERE age > 18;

Conclusion

The fmt.Sprintf function is used for creating formatted strings in Go. It provides flexibility and precision in formatting, making it ideal for building dynamic strings and constructing messages that require variable data. By using fmt.Sprintf, you can improve the readability and maintainability of your Go code when handling strings and data formatting.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare