"Testing1"
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
| Verb | Description |
|---|---|
%v | Default representation of a value |
%+v | Like %v, but includes struct field names |
%#v | Go syntax representation of a value |
%T | Type 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
| Verb | Description |
|---|---|
%b | Binary representation |
%d | Decimal representation |
%o | Octal representation |
%x | Lowercase hexadecimal |
%X | Uppercase 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
| Verb | Description |
|---|---|
%e | Scientific notation (lowercase e) |
%E | Scientific notation (uppercase E) |
%f | Decimal representation |
%g | Uses %e or %f, whichever is more concise |
%G | Uses %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
| Verb | Description |
|---|---|
%s | Prints a string |
%q | Quoted string with escape sequences |
%x | Hexadecimal 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
| Verb | Description |
|---|---|
%t | Prints 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
| Verb | Description |
|---|---|
%p | Prints 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
| Verb | Description |
|---|---|
%5d | Minimum width of 5 for integers |
%10s | Minimum width of 10 for strings |
%.2f | Two decimal places for floats |
%6.2f | Width 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.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand GO Tutorial visually:
What You'll Learn:
- 📌 Learn Golang 4: Formatting verbs (learn Go programming language)
- 📌 Golang Tutorial - Channels - 26