codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

35 / 72 topics
27Standard Library Overview28IO Package29Net Package30HTTP Package31JSON Package32XML Package33Time Package34OS Package35Fmt Package36Math Package37Regexp Package38Log Package39Flag Package40Context Package41Embed Package
Tutorials/Go (Golang)/Fmt Package
🐹Go (Golang)

Fmt Package

Updated 2026-04-20
3 min read

Fmt Package

The fmt package is one of the most fundamental and widely used packages in the Go programming language. It provides functions for formatted I/O operations, such as printing to standard output and reading from standard input. This guide will cover the basics and advanced features of the fmt package, including formatting verbs, flags, and best practices.

Overview

The fmt package is part of the Go standard library and includes several functions that are essential for debugging, logging, and user interaction. Some of the most commonly used functions include:

  • Print, Printf, Println: For printing to standard output.
  • Scan, Scanf, Scanln: For reading from standard input.

Basic Printing Functions

Print

The fmt.Print function prints its arguments without a trailing newline. It is useful when you want to concatenate multiple outputs on the same line.

package main

import (
    "fmt"
)

func main() {
    fmt.Print("Hello, ")
    fmt.Print("World!")
}

Output:

Hello, World!

Println

The fmt.Println function prints its arguments followed by a newline. It is the most commonly used function for simple output.

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello, World!")
}

Output:

Hello, World!

Printf

The fmt.Printf function formats its arguments according to a format specifier and prints the result. It is highly versatile and allows for complex formatting.

package main

import (
    "fmt"
)

func main() {
    name := "Alice"
    age := 30
    fmt.Printf("Name: %s, Age: %d\n", name, age)
}

Output:

Name: Alice, Age: 30

Formatting Verbs

The fmt package uses verbs to specify how each argument should be formatted. Here are some common verbs:

  • %v: The default format for the value.
  • %+v: A Go-syntax representation of the value.
  • %#v: A Go-syntax representation of the value, with type information.
  • %T: The type of the value.
  • %d: Decimal integer.
  • %f: Floating-point number.
  • %s: String.
  • %t: Boolean.

Examples

package main

import (
    "fmt"
)

func main() {
    name := "Alice"
    age := 30
    fmt.Printf("Name: %v, Age: %d\n", name, age)
    fmt.Printf("Name: %+v, Age: %d\n", name, age)
    fmt.Printf("Name: %#v, Age: %d\n", name, age)
    fmt.Printf("Type of Name: %T\n", name)
}

Output:

Name: Alice, Age: 30
Name: Alice, Age: 30
Name: "Alice", Age: 30
Type of Name: string

Flags

Flags can be used with formatting verbs to modify their behavior. Common flags include:

  • -: Left justify the output.
  • +: Always show a sign for numeric values.
  • #: Alternate format (e.g., hexadecimal for integers).
  • 0: Pad with zeros instead of spaces.

Examples

package main

import (
    "fmt"
)

func main() {
    num := 42
    fmt.Printf("%5d\n", num)      // Right justify in a field width of 5
    fmt.Printf("%-5d\n", num)     // Left justify in a field width of 5
    fmt.Printf("%+d\n", num)      // Show the sign
    fmt.Printf("%#x\n", num)      // Hexadecimal with "0x" prefix
    fmt.Printf("%05d\n", num)     // Pad with zeros to a width of 5
}

Output:

   42
42   
+42
0x2a
00042

Reading Input

The fmt package also provides functions for reading input from the standard input.

Scan

The fmt.Scan function reads space-separated values from standard input and stores them in the variables provided.

package main

import (
    "fmt"
)

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

Scanf

The fmt.Scanf function reads formatted input from standard input.

package main

import (
    "fmt"
)

func main() {
    var name string
    var age int
    fmt.Print("Enter your name and age (e.g., Alice 30): ")
    fmt.Scanf("%s %d", &name, &age)
    fmt.Printf("Hello, %s! You are %d years old.\n", name, age)
}

Scanln

The fmt.Scanln function reads space-separated values from standard input until a newline is encountered.

package main

import (
    "fmt"
)

func main() {
    var name string
    fmt.Print("Enter your name: ")
    fmt.Scanln(&name)
    fmt.Printf("Hello, %s!\n", name)
}

Best Practices

  1. Use Printf for Complex Formatting: When you need to format strings with multiple variables or complex types, use fmt.Printf.
  2. Avoid Mixing Different Print Functions: Stick to one style of printing (e.g., all Println) to maintain consistency in your code.
  3. Handle Errors Properly: When reading input, always check for errors to handle invalid inputs gracefully.
  4. Use Verbs Appropriately: Choose the correct verb based on the type and format you need. For example, use %s for strings and %d for integers.

Conclusion

The fmt package is a powerful tool for handling I/O operations in Go. By understanding its functions, formatting verbs, flags, and best practices, you can write clean, efficient, and readable code. Whether you are debugging, logging, or interacting with users, the fmt package has got you covered.

Further Reading

  • Go by Example: fmt
  • The Go Programming Language Specification - Printing

This comprehensive guide should provide a solid foundation for using the fmt package in your Go projects. Happy coding!


PreviousOS PackageNext Math Package

Recommended Gear

OS PackageMath Package