Format specifiers in Go

Format specifiers in Go

fmt.Printf in Go is a powerful function used to format and print data. It is similar to the printf function in languages like C, and it uses format specifiers to determine how the provided arguments should be formatted in the output string.

Common Format Specifiers:

  1. %v: The default format, depending on the argument type.

  2. %+v: Same as %v but will include the field names in the default formatter for structs.

  3. %#v: A representation of the Go-syntax for the value.

  4. %T: Type of the value.

  5. %d: Integer (base 10).

  6. %b: Integer (base 2, binary).

  7. %o: Integer (base 8, octal).

  8. %x, %X: Integer (base 16, hex). Uses lowercase or uppercase letters for a-f respectively.

  9. %f, %F: Floating-point number.

  10. %e, %E: Scientific notation.

  11. %g: Decides %e or %f based on magnitude.

  12. %s: String.

  13. %q: A double-quoted string safely escaped with Go syntax.

  14. %p: Pointer.

  15. %c: The character represented by the corresponding Unicode code point.

Using fmt.Printf:

Here's how you'd use some of these format specifiers with fmt.Printf:

package main

import "fmt"

func main() {
    name := "Alice"
    age := 30
    height := 1.68

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

This program will print: Name: Alice, Age: 30, Height: 1.680000 meters.

Exercises:

  1. Basic Formatting:

    • Declare an integer, a float, and a string. Print them using %d, %f, and %s.
  2. Go Syntax Formatting:

    • Declare a slice of integers and print it using %v and %#v. Observe the difference.
  3. Printing Types:

    • Declare a few variables of different types (int, float64, string, bool). Use %T to print their types.
  4. Scientific Notation:

    • Declare a very small float (like 0.000000123) and a large float (like 1234567890.0). Print them using %e, %E, and %g.
  5. Structs with Field Names:

    • Declare a struct representing a book with fields Title and Author. Initialize an instance of the struct and print it using %v and %+v.
  6. Escape Sequences in Strings:

    • Declare a string containing quotes and backslashes. Print it using %s and %q.
  7. Pointer Addresses:

    • Declare a variable and print its memory address using %p.

Solutions

Let's solve each exercise.

1. Basic Formatting:

package main

import "fmt"

func main() {
    number := 42
    piValue := 3.141592
    word := "Hello"

    fmt.Printf("Integer: %d, Float: %f, String: %s\n", number, piValue, word)
    // Output: Integer: 42, Float: 3.141592, String: Hello
}

2. Go Syntax Formatting:

package main

import "fmt"

func main() {
    numbers := []int{1, 2, 3, 4, 5}

    fmt.Printf("Default: %v\n", numbers)  // Output: Default: [1 2 3 4 5]
    fmt.Printf("Go syntax: %#v\n", numbers)  // Output: Go syntax: []int{1, 2, 3, 4, 5}
}

3. Printing Types:

package main

import "fmt"

func main() {
    integer := 42
    floating := 3.14
    text := "Golang"
    truthy := true

    fmt.Printf("Type of integer: %T\n", integer)   // Output: Type of integer: int
    fmt.Printf("Type of floating: %T\n", floating)  // Output: Type of floating: float64
    fmt.Printf("Type of text: %T\n", text)   // Output: Type of text: string
    fmt.Printf("Type of truthy: %T\n", truthy)  // Output: Type of truthy: bool
}

4. Scientific Notation:

package main

import "fmt"

func main() {
    small := 0.000000123
    large := 1234567890.0

    fmt.Printf("Small in %e\n", small)  // Output: Small in 1.230000e-07
    fmt.Printf("Small in %E\n", small)  // Output: Small in 1.230000E-07
    fmt.Printf("Large in %g\n", large)  // Output: Large in 1.23456789e+09
}

5. Structs with Field Names:

package main

import "fmt"

type Book struct {
    Title  string
    Author string
}

func main() {
    myBook := Book{Title: "Go Programming", Author: "John Doe"}

    fmt.Printf("Default: %v\n", myBook) // Output: Default: {Go Programming John Doe}
    fmt.Printf("With field names: %+v\n", myBook) // Output: With field names: {Title:Go Programming Author:John Doe}
}

6. Escape Sequences in Strings:

package main

import "fmt"

func main() {
    sentence := "She said, \"Hello, World!\" and \\ disappeared."

    fmt.Printf("Normal: %s\n", sentence)  
    // Output: Normal: She said, "Hello, World!" and \ disappeared.

    fmt.Printf("Quoted: %q\n", sentence) 
    // Output: Quoted: "She said, \"Hello, World!\" and \\ disappeared."
}

7. Pointer Addresses:

package main

import "fmt"

func main() {
    number := 42

    fmt.Printf("Address of number: %p\n", &number) // This will print the memory address of the number.
}

These solutions demonstrate how to utilize the format specifiers with the fmt.Printf function in Go. If you run and understand each one, you'll have a solid grasp of this concept!

References

  1. Go fmt package