Introduction
String formatting is a common requirement in many programming tasks, such as generating reports, creating log messages, or constructing user-readable output. Go provides robust support for string formatting through the fmt
package. In this chapter, you will learn the basics of string formatting in Go, including common formatting verbs, functions for formatting strings, and examples of how to format various types of data.
Common Formatting Verbs
Formatting verbs in Go are special characters that specify how to format a value. Here are some of the most commonly used verbs:
%v
– Default format%+v
– Include field names in structs%#v
– Go-syntax representation%T
– Type of the value%%
– Literal percent sign
For strings:
%s
– String or slice of bytes%q
– Double-quoted string
For integers:
%d
– Base 10%b
– Base 2%o
– Base 8%x
– Base 16 (lowercase)%X
– Base 16 (uppercase)
For floating-point and complex numbers:
%f
– Decimal point, no exponent%e
– Scientific notation (lowercase)%E
– Scientific notation (uppercase)
For pointers:
%p
– Base 16 address, with leading 0x
Formatting Functions
The fmt
package provides several functions for formatting strings:
fmt.Sprintf
– Returns a formatted stringfmt.Fprintf
– Writes a formatted string to aio.Writer
fmt.Printf
– Writes a formatted string to standard outputfmt.Sprint
– Returns a string without formattingfmt.Fprint
– Writes a string to aio.Writer
without formattingfmt.Print
– Writes a string to standard output without formatting
Examples
Basic String Formatting
Example:
package main
import (
"fmt"
)
func main() {
name := "Alice"
age := 30
str := fmt.Sprintf("Name: %s, Age: %d", name, age)
fmt.Println(str) // Output: Name: Alice, Age: 30
}
In this example, fmt.Sprintf
is used to format a string with a name and age.
Formatting Structs
Example:
package main
import (
"fmt"
)
type Person struct {
Name string
Age int
}
func main() {
person := Person{"Alice", 30}
fmt.Printf("%v\n", person) // Output: {Alice 30}
fmt.Printf("%+v\n", person) // Output: {Name:Alice Age:30}
fmt.Printf("%#v\n", person) // Output: main.Person{Name:"Alice", Age:30}
fmt.Printf("%T\n", person) // Output: main.Person
}
In this example, different verbs are used to format a struct in various ways.
Formatting Numbers
Example:
package main
import (
"fmt"
)
func main() {
number := 123
fmt.Printf("Decimal: %d\n", number) // Output: Decimal: 123
fmt.Printf("Binary: %b\n", number) // Output: Binary: 1111011
fmt.Printf("Octal: %o\n", number) // Output: Octal: 173
fmt.Printf("Hex: %x\n", number) // Output: Hex: 7b
fmt.Printf("Hex (uppercase): %X\n", number) // Output: Hex (uppercase): 7B
}
In this example, different verbs are used to format an integer in various number bases.
Formatting Floating-Point Numbers
Example:
package main
import (
"fmt"
)
func main() {
number := 123.456
fmt.Printf("Default: %v\n", number) // Output: Default: 123.456
fmt.Printf("Decimal: %f\n", number) // Output: Decimal: 123.456000
fmt.Printf("Scientific: %e\n", number) // Output: Scientific: 1.234560e+02
fmt.Printf("Scientific (uppercase): %E\n", number) // Output: Scientific (uppercase): 1.234560E+02
}
In this example, different verbs are used to format a floating-point number in various ways.
Formatting Pointers
Example:
package main
import (
"fmt"
)
func main() {
number := 123
pointer := &number
fmt.Printf("Pointer: %p\n", pointer) // Output: Pointer: 0xc000012080
}
In this example, the %p
verb is used to format a pointer.
Padding and Alignment
Example:
package main
import (
"fmt"
)
func main() {
number := 123
fmt.Printf("Right-aligned: %6d\n", number) // Output: Right-aligned: 123
fmt.Printf("Left-aligned: %-6d\n", number) // Output: Left-aligned: 123
fmt.Printf("Padded with zeros: %06d\n", number) // Output: Padded with zeros: 000123
}
In this example, different verbs are used to format an integer with padding and alignment.
Formatting with Width and Precision
Example:
package main
import (
"fmt"
)
func main() {
number := 123.456
fmt.Printf("Default: %f\n", number) // Output: Default: 123.456000
fmt.Printf("Width 10: %10f\n", number) // Output: Width 10: 123.456000
fmt.Printf("Precision 2: %.2f\n", number) // Output: Precision 2: 123.46
fmt.Printf("Width 10, Precision 2: %10.2f\n", number) // Output: Width 10, Precision 2: 123.46
}
In this example, different verbs are used to format a floating-point number with specific width and precision.
Conclusion
String formatting in Go is a powerful feature provided by the fmt
package. By understanding and using the various formatting verbs and functions, you can generate well-formatted strings for different types of data, improving the readability and presentation of your program’s output. Whether you are working with basic strings, numbers, structs, or pointers, Go’s string formatting capabilities can help you achieve the desired output.