Golang fmt Printf Function

The fmt.Printf function in Golang is part of the fmt package and is used to output formatted strings to the standard output (usually the console). It provides precise control over the output format by using format specifiers, allowing you to format strings, numbers, and other data types in a structured way.

Table of Contents

  1. Introduction
  2. Printf Function Syntax
  3. Examples
    • Basic Usage
    • Formatting Numbers
    • Aligning Text
  4. Real-World Use Case
  5. Conclusion

Introduction

The fmt.Printf function is used for formatted output in Go programs. It allows you to specify exactly how you want your data to be displayed by using format specifiers. This is useful for creating well-formatted console output, debugging, and logging. Unlike fmt.Print, fmt.Printf requires a format string that dictates how the data will be printed.

Printf Function Syntax

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

func Printf(format string, a ...interface{}) (n int, err error)

Parameters:

  • format: A string containing format specifiers that define how the arguments should be formatted.
  • a: The data to be formatted and printed.

Returns:

  • n: The number of bytes written.
  • err: An error if one occurred during writing.

Common Format Specifiers:

  • %s: String
  • %d: Integer (base 10)
  • %f: Floating-point number
  • %t: Boolean
  • %v: Default format for the type
  • %T: Type of the value
  • %%: Literal percent sign

Examples

Basic Usage

This example demonstrates how to use the fmt.Printf function to format and output text and numbers.

Example

package main

import (
	"fmt"
)

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

	// Use fmt.Printf to format a string with placeholders
	fmt.Printf("Name: %s, Age: %d\n", name, age)
}

Output:

Name: Alice, Age: 30

Formatting Numbers

You can use fmt.Printf to format numbers with different precision and padding.

Example

package main

import (
	"fmt"
)

func main() {
	price := 123.456

	// Format floating-point numbers with two decimal places
	fmt.Printf("Price: $%.2f\n", price)

	// Format numbers with leading zeros
	number := 42
	fmt.Printf("Number: %04d\n", number)
}

Output:

Price: $123.46
Number: 0042

Aligning Text

fmt.Printf can also be used to align text by specifying the width of the output.

Example

package main

import (
	"fmt"
)

func main() {
	product := "Laptop"
	price := 999.99

	// Align text to the right with a specified width
	fmt.Printf("%-10s $%6.2f\n", product, price)

	product2 := "Smartphone"
	price2 := 699.99

	fmt.Printf("%-10s $%6.2f\n", product2, price2)
}

Output:

Laptop     $999.99
Smartphone $699.99

Real-World Use Case

Logging Information

In real-world applications, fmt.Printf can be used to create formatted log messages with dynamic data.

Example

package main

import (
	"fmt"
	"time"
)

func main() {
	logLevel := "INFO"
	message := "Application started"
	timestamp := time.Now().Format("2006-01-02 15:04:05")

	// Use fmt.Printf to format a log message
	fmt.Printf("[%s] %s: %s\n", timestamp, logLevel, message)
}

Output:

[2024-08-06 14:23:45] INFO: Application started

Conclusion

The fmt.Printf function is a versatile function for creating formatted output in Go programs. It provides precise control over how data is displayed, making it ideal for debugging, logging, and generating reports. By using fmt.Printf, you can create clear and structured console output in your applications.

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