0PricingLogin
Go Academy · Lesson

fmt and strings Packages

Formatting output and manipulating strings

The fmt Package Overview

The fmt package implements formatted I/O. Key functions:

  • fmt.Print, fmt.Println, fmt.Printf — write to stdout
  • fmt.Sprint, fmt.Sprintf, fmt.Sprintln — return strings
  • fmt.Fprint, fmt.Fprintf — write to any io.Writer
  • fmt.Scan, fmt.Scanf — read from stdin

fmt Format Verbs

Common format verbs for fmt.Printf:

package main
import "fmt"

func main() {
    name := "Gopher"
    age := 15
    score := 98.6

    fmt.Printf("%s is %d years old\n", name, age)
    fmt.Printf("Score: %.1f%%\n", score)
    fmt.Printf("Type: %T, Value: %v\n", age, age)
    fmt.Printf("Hex: %x, Binary: %b\n", 255, 255)
    fmt.Printf("Padded: %10s|%-10s\n", name, name)
}

All lessons in this course

  1. fmt and strings Packages
  2. strconv, math and sort
  3. time Package Essentials
  4. os and filepath Packages
← Back to Go Academy