Formatting Verbs


Formatting Verbs

Go’s fmt package provides powerful formatting tools, including formatting verbs, which define how values are displayed in formatted strings. These verbs are used with functions like fmt.Printf, fmt.Sprintf, and fmt.Fprintf to format and output text efficiently.


Commonly Used Formatting Verbs in Go

Below are various formatting verbs categorized by data types along with syntax and examples.


1. General Formatting Verbs

VerbDescription
%vDefault representation of a value
%+vLike %v, but includes struct field names
%#vGo syntax representation of a value
%TType of the value
%%Prints a literal % symbol

Example:

package main

import "fmt"

func main() {
    type Person struct {
        Name string
        Age  int
    }
    
    p := Person{"Alice", 25}

    fmt.Printf("Default format: %v\n", p)
    fmt.Printf("With field names: %+v\n", p)
    fmt.Printf("Go syntax format: %#v\n", p)
    fmt.Printf("Type of variable: %T\n", p)
    fmt.Printf("Percentage sign: %%\n")
}

Output:

Default format: {Alice 25}
With field names: {Name:Alice Age:25}
Go syntax format: main.Person{Name:"Alice", Age:25}
Type of variable: main.Person
Percentage sign: %

2. Integer Formatting Verbs

VerbDescription
%bBinary representation
%dDecimal representation
%oOctal representation
%xLowercase hexadecimal
%XUppercase hexadecimal

Example:

package main

import "fmt"

func main() {
    num := 255

    fmt.Printf("Binary: %b\n", num)
    fmt.Printf("Decimal: %d\n", num)
    fmt.Printf("Octal: %o\n", num)
    fmt.Printf("Hexadecimal (lower): %x\n", num)
    fmt.Printf("Hexadecimal (upper): %X\n", num)
}

Output:

Binary: 11111111
Decimal: 255
Octal: 377
Hexadecimal (lower): ff
Hexadecimal (upper): FF

3. Floating-Point Formatting Verbs

VerbDescription
%eScientific notation (lowercase e)
%EScientific notation (uppercase E)
%fDecimal representation
%gUses %e or %f, whichever is more concise
%GUses %E or %f, whichever is more concise

Example:

package main

import "fmt"

func main() {
    num := 123.456

    fmt.Printf("Scientific notation (lower): %e\n", num)
    fmt.Printf("Scientific notation (upper): %E\n", num)
    fmt.Printf("Floating-point: %f\n", num)
    fmt.Printf("Compact floating-point: %g\n", num)
    fmt.Printf("Compact floating-point (uppercase): %G\n", num)
}

Output:

Scientific notation (lower): 1.234560e+02
Scientific notation (upper): 1.234560E+02
Floating-point: 123.456000
Compact floating-point: 123.456
Compact floating-point (uppercase): 123.456

4. String and Character Formatting Verbs

VerbDescription
%sPrints a string
%qQuoted string with escape sequences
%xHexadecimal encoding of a string

Example:

package main

import "fmt"

func main() {
    text := "GoLang"

    fmt.Printf("String: %s\n", text)
    fmt.Printf("Quoted String: %q\n", text)
    fmt.Printf("Hex Encoding: %x\n", text)
}

Output:

String: GoLang
Quoted String: "GoLang"
Hex Encoding: 476f4c616e67

5. Boolean Formatting Verbs

VerbDescription
%tPrints true or false

Example:

package main

import "fmt"

func main() {
    val := true

    fmt.Printf("Boolean: %t\n", val)
}

Output:

Boolean: true

6. Pointer Formatting Verbs

VerbDescription
%pPrints a pointer’s memory address

Example:

package main

import "fmt"

func main() {
    num := 10
    fmt.Printf("Memory Address: %p\n", &num)
}

Output:

Memory Address: 0xc0000140a0 (example, varies per run)

7. Width and Precision Formatting

VerbDescription
%5dMinimum width of 5 for integers
%10sMinimum width of 10 for strings
%.2fTwo decimal places for floats
%6.2fWidth 6 and 2 decimal places

Example:

package main

import "fmt"

func main() {
    num := 42
    price := 3.14159
    text := "Go"

    fmt.Printf("Integer with width: |%5d|\n", num)
    fmt.Printf("String with width: |%10s|\n", text)
    fmt.Printf("Float with precision: |%.2f|\n", price)
    fmt.Printf("Float with width and precision: |%6.2f|\n", price)
}

Output:

Integer with width: |   42|
String with width: |        Go|
Float with precision: |3.14|
Float with width and precision: |  3.14|

Conclusion

Go’s formatting verbs allow precise control over output formatting. They are widely used in logging, debugging, and structured data output. The fmt package’s functions, such as Printf, Sprintf, and Fprintf, leverage these verbs to format data before displaying or storing it.

PreviousNext