Go Formatting Verbs:

Formatting verbs in Go are special symbols used in functions like fmt.Printf to format variables. They start with a percent sign (%) and are followed by a character that determines how the value is formatted.

1. Common Verbs

  • %v — Default format
  • %+v — Adds field names for structs
  • %#v — Go-syntax representation
  • %T — Type of the value
go
package main

import "fmt"

type Person struct { Name string; Age int }

func main() {
  p := Person{Name: "John", Age: 30}
  fmt.Printf("%v\n", p)
  fmt.Printf("%+v\n", p)
  fmt.Printf("%#v\n", p)
  fmt.Printf("%T\n", p)
}

2. Integer Verbs

  • %d — Decimal
  • %b — Binary
  • %o — Octal
  • %x — Hex (lowercase)
  • %X — Hex (uppercase)
go
package main

import "fmt"

func main() {
  n := 42
  fmt.Printf("Decimal: %d\n", n)
  fmt.Printf("Binary: %b\n", n)
  fmt.Printf("Octal: %o\n", n)
  fmt.Printf("Hex: %x\n", n)
  fmt.Printf("HEX: %X\n", n)
}

3. Floating-Point Verbs

  • %f — Decimal point but no exponent
  • %e — Scientific notation (e.g., -1.234456e+78)
  • %E — Scientific notation with E
go
package main

import "fmt"

func main() {
  pi := 3.1415926535
  fmt.Printf("Float: %f\n", pi)
  fmt.Printf("Scientific (e): %e\n", pi)
  fmt.Printf("Scientific (E): %E\n", pi)
}

4. String and Boolean Verbs

  • %s — String
  • %q — Double-quoted string
  • %t — Boolean
go
package main

import "fmt"

func main() {
  str := "GoLang"
  flag := true
  fmt.Printf("String: %s\n", str)
  fmt.Printf("Quoted: %q\n", str)
  fmt.Printf("Boolean: %t\n", flag)
}

Summary

Formatting verbs give you fine control over how output is displayed in Go. They are essential for logging, debugging, and structured output formatting.