Sling Academy
Home/Golang/Working with the `fmt` Package for Formatted I/O in Go

Working with the `fmt` Package for Formatted I/O in Go

Last updated: November 26, 2024

Introduction to the `fmt` Package

The fmt package in Go is essential for formatted I/O operations such as printing and scanning. This package provides the most common operations you're going to use for console output and simple formatted input.

Printing with `fmt`

The `fmt` package offers various functions for printing:

  • fmt.Print()
  • fmt.Println()
  • fmt.Printf()

Using Print and Println

The simplest functions in the fmt package are Print() and Println(), which print text to the standard output.

package main

import "fmt"

func main() {
    fmt.Print("Hello, World!")
    fmt.Println("Hello, World!") // This adds a new line at the end
}

Formatted Printing with Printf

The Printf function allows for formatted strings using verbs, similar to C's printf:


func main() {
    age := 30
    name := "John Doe"
    fmt.Printf("%s is %d years old.\n", name, age)
}

Formatting Verbs

Some of the most common verbs used with Printf include:

  • %s - for strings.
  • %d - for integers.
  • %f - for floating-point numbers.
  • %.2f - for formatting floats to 2 decimal points.

func main() {
    price := 19.99
    discount := 0.15
    fmt.Printf("Price: $%.2f\nDiscount: %.0f%%\n", price, discount*100)
}

Reading Input with `fmt`

The fmt package can also read input from users. The primary function for this is Scanln:


func main() {
    var name string
    var age int
    fmt.Print("Enter your name and age: ")
    fmt.Scanln(&name, &age)
    fmt.Printf("Hello %s, you are %d years old.\n", name, age)
}

Conclusion

The fmt package is a convenient tool in Go for handling basic formatted input and output. It's straightforward for those familiar with formatted I/O from other languages, enabling easy adaptation and productivity.

Next Article: Handling Files and Directories Using the `os` Package in Go

Previous Article: Introduction to the Core Packages in Go: An Overview

Series: Working with Core package in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant