Go Formatting Verbs

Welcome to The Coding College! Formatting verbs in Go (Golang) are essential for creating structured and dynamic output. By using verbs with the fmt package, you can customize how data is displayed, ensuring precision and clarity.

What Are Formatting Verbs?

Formatting verbs are placeholders used in fmt functions like fmt.Printf and fmt.Sprintf. They specify how values should be formatted for output.

Commonly Used Formatting Verbs

1. General Verbs

These verbs apply to most data types:

VerbDescriptionExample Output
%vDefault format for the valuefmt.Printf("%v", 42) → 42
%+vAdds field names (structs)fmt.Printf("%+v", person)
%#vGo-syntax representationfmt.Printf("%#v", person)
%TType of the valuefmt.Printf("%T", 42) → int
%%Literal percent signfmt.Printf("%%") → %

2. Integer Verbs

Use these for formatting integers:

VerbDescriptionExample Output
%dDecimal (base 10)fmt.Printf("%d", 42) → 42
%bBinaryfmt.Printf("%b", 5) → 101
%oOctalfmt.Printf("%o", 9) → 11
%xHexadecimal (lowercase)fmt.Printf("%x", 15) → f
%XHexadecimal (uppercase)fmt.Printf("%X", 15) → F
%cUnicode characterfmt.Printf("%c", 65) → A
%UUnicode formatfmt.Printf("%U", 65) → U+0041

3. Floating-Point and Complex Verbs

These are used for floating-point numbers and complex values:

VerbDescriptionExample Output
%fDecimal formatfmt.Printf("%f", 3.14) → 3.140000
%.nfSpecify decimal places (precision)fmt.Printf("%.2f", 3.14) → 3.14
%eScientific notation (lowercase)fmt.Printf("%e", 3.14) → 3.140000e+00
%EScientific notation (uppercase)fmt.Printf("%E", 3.14) → 3.140000E+00
%gCompact format (scientific/decimal)fmt.Printf("%g", 3.14) → 3.14

4. String and Byte Slice Verbs

These apply to strings and byte slices:

VerbDescriptionExample Output
%sStringfmt.Printf("%s", "Go") → Go
%qQuoted stringfmt.Printf("%q", "Go") → "Go"
%xHex dump (lowercase)fmt.Printf("%x", "Go") → 476f
%XHex dump (uppercase)fmt.Printf("%X", "Go") → 476F

5. Pointer Verbs

Pointers can also be formatted:

VerbDescriptionExample Output
%pPointer addressfmt.Printf("%p", &x) → 0xc0000100a0

Controlling Width and Alignment

You can specify field width and alignment with numbers in the format string:

1. Width

  • Minimum width for a field:
fmt.Printf("%10s", "Go") // Right-align

Output:

        Go

2. Left-Align

  • Use - to left-align:
fmt.Printf("%-10s is great!", "Go")

Output:

Go         is great!

3. Width and Precision for Floats

  • Specify both width and precision:
fmt.Printf("%10.2f", 3.14159)

Output:

      3.14

Practical Examples

Example 1: Format Various Types

package main

import "fmt"

func main() {
    var name = "Alice"
    var age = 30
    var height = 5.75

    fmt.Printf("Name: %s, Age: %d, Height: %.2f\n", name, age, height)
}

Output:

Name: Alice, Age: 30, Height: 5.75

Example 2: Inspect Type and Value

package main

import "fmt"

func main() {
    var x = 42
    fmt.Printf("Value: %v, Type: %T\n", x, x)
}

Output:

Value: 42, Type: int

Example 3: Enumerate Integers

package main

import "fmt"

func main() {
    for i := 1; i <= 5; i++ {
        fmt.Printf("Decimal: %d, Binary: %b, Hex: %x\n", i, i, i)
    }
}

Output:

Decimal: 1, Binary: 1, Hex: 1
Decimal: 2, Binary: 10, Hex: 2
Decimal: 3, Binary: 11, Hex: 3
Decimal: 4, Binary: 100, Hex: 4
Decimal: 5, Binary: 101, Hex: 5

Best Practices

  1. Choose the Right Verb: Ensure the verb matches the data type.
  2. Use Precision for Floats: Avoid unnecessary decimals with %.nf.
  3. Combine Verbs for Structs: Use %+v for detailed output of structs.
  4. Inspect Types with %T: Debug by checking variable types.

Conclusion

Formatting verbs in Go provide powerful tools for customizing output, making your programs more user-friendly and professional. By mastering these verbs, you can write better code that communicates effectively.

Leave a Comment