In Go, the fmt.Printf()
function supports a variety of formatting verbs that allow you to control how values are formatted and displayed in the output. Here are some commonly used formatting verbs:
%d
or%v
: Used for formatting integer values.
package main
import "fmt"
func main() {
num := 42
fmt.Printf("Decimal: %d\n", num)
fmt.Printf("Default: %v\n", num)
}
Output:
Decimal: 42
Default: 42
%f
: Used for formatting floating-point values.
package main
import "fmt"
func main() {
num := 3.14159
fmt.Printf("Float: %f\n", num)
}
Output:
Float: 3.141590
%s
: Used for formatting string values.
package main
import "fmt"
func main() {
name := "Alice"
fmt.Printf("Name: %s\n", name)
}
Output:
Name: Alice
%t
: Used for formatting boolean values.
package main
import "fmt"
func main() {
status := true
fmt.Printf("Status: %t\n", status)
}
Output:
Status: true
%c
: Used for formatting a single character.
package main
import "fmt"
func main() {
char := 'A'
fmt.Printf("Character: %c\n", char)
}
Output:
Character: A
These are just a few examples of formatting verbs available in Go. There are additional verbs for different data types and formatting options. You can also specify additional flags and width/precision specifiers to further customize the output format. The fmt
package documentation provides a comprehensive list of formatting verbs and options in Go.
General Formatting Verbs In Go.
In Go, the fmt
package provides a variety of formatting verbs that you can use with the Printf()
family of functions to control the formatting and display of values. Here are some commonly used formatting verbs:
%v
– The default format:
- For struct types, it displays the values of the fields recursively.
- For arrays, slices, and maps, it displays the elements/values in a default format.
- For other types, it provides a reasonable default representation.
%+v
– The+
flag adds field names when printing structs:
- For struct types, it displays the field names along with their values.
%#v
– The#
flag adds Go-syntax representation of the value:
- It displays the Go-syntax representation of the value, which can be used to recreate the value.
%T
– Displays the type of the value:
- It prints the type of the value.
%d
– Integer formatting:
- It formats an integer value in decimal notation.
%f
– Floating-point formatting:
- It formats a floating-point value in decimal notation.
%s
– String formatting:
- It formats a string value as is.
%t
– Boolean formatting:
- It formats a boolean value as
true
orfalse
.
%p
– Pointer formatting:
- It formats a pointer value in hexadecimal notation.
These are just a few examples of the formatting verbs available in Go. You can combine these verbs with additional flags and width/precision specifiers to further customize the output format.
For a comprehensive list of formatting verbs and options, you can refer to the fmt
package documentation: https://golang.org/pkg/fmt/
Integer Formatting Verbs In Go and Example.
In Go, there are several formatting verbs available for formatting integer values. Here are the commonly used integer formatting verbs along with examples:
%d
– Decimal notation:
- It formats an integer value in decimal notation.
package main
import "fmt"
func main() {
num := 42
fmt.Printf("Decimal: %d\n", num)
}
Output:
Decimal: 42
%b
– Binary notation:
- It formats an integer value in binary notation.
package main
import "fmt"
func main() {
num := 42
fmt.Printf("Binary: %b\n", num)
}
Output:
Binary: 101010
%o
– Octal notation:
- It formats an integer value in octal notation.
package main
import "fmt"
func main() {
num := 42
fmt.Printf("Octal: %o\n", num)
}
Output:
Octal: 52
%x
or%X
– Hexadecimal notation:
%x
formats an integer value in lowercase hexadecimal notation.%X
formats an integer value in uppercase hexadecimal notation.
package main
import "fmt"
func main() {
num := 42
fmt.Printf("Hexadecimal (lowercase): %x\n", num)
fmt.Printf("Hexadecimal (uppercase): %X\n", num)
}
Output:
Hexadecimal (lowercase): 2a
Hexadecimal (uppercase): 2A
%c
– Unicode character representation:
- It interprets an integer value as a Unicode code point and formats it as a character.
package main
import "fmt"
func main() {
unicode := 65
fmt.Printf("Character: %c\n", unicode)
}
Output:
Character: A
These are some examples of integer formatting verbs in Go. You can use these verbs with additional flags and width specifiers to further customize the output format according to your needs.
String Formatting Verbs In Go and Example.
In Go, there are several formatting verbs available for formatting string values. Here are the commonly used string formatting verbs along with examples:
%s
– String representation:
- It formats a string value as is.
package main
import "fmt"
func main() {
str := "Hello, World!"
fmt.Printf("String: %s\n", str)
}
Output:
String: Hello, World!
%q
– Quoted string representation:
- It formats a string value as a quoted string, with special characters escaped.
package main
import "fmt"
func main() {
str := "Hello, World!"
fmt.Printf("Quoted String: %q\n", str)
}
Output:
Quoted String: "Hello, World!"
%x
or%X
– Hexadecimal representation:
%x
formats a string value as lowercase hexadecimal.%X
formats a string value as uppercase hexadecimal.
package main
import "fmt"
func main() {
str := "Hello"
fmt.Printf("Hexadecimal (lowercase): %x\n", str)
fmt.Printf("Hexadecimal (uppercase): %X\n", str)
}
Output:
Hexadecimal (lowercase): 48656c6c6f
Hexadecimal (uppercase): 48656C6C6F
These are some examples of string formatting verbs in Go. You can use these verbs with additional flags and width specifiers to further customize the output format according to your needs.
Boolean Formatting Verbs and Examples.
In Go, there is a formatting verb specifically designed for formatting boolean values. Here is the boolean formatting verb along with an example:
%t
– Boolean representation:
- It formats a boolean value as
true
orfalse
.
package main
import "fmt"
func main() {
status := true
fmt.Printf("Status: %t\n", status)
}
Output:
Status: true
The %t
verb is used to format boolean values. It automatically converts the boolean value to the corresponding string representation of true
or false
.
This is an example of a boolean formatting verb in Go. You can use this verb to format boolean values in a desired format as needed.
Float Formatting Verbs In Go and Example
In Go, there are several formatting verbs available for formatting floating-point values. Here are the commonly used float formatting verbs along with examples:
%f
– Default float representation:
- It formats a floating-point value in decimal notation.
package main
import "fmt"
func main() {
num := 3.14159
fmt.Printf("Float: %f\n", num)
}
Output:
Float: 3.141590
%e
or%E
– Scientific notation:
%e
formats a floating-point value in lowercase scientific notation.%E
formats a floating-point value in uppercase scientific notation.
package main
import "fmt"
func main() {
num := 3.14159
fmt.Printf("Scientific (lowercase): %e\n", num)
fmt.Printf("Scientific (uppercase): %E\n", num)
}
Output:
Scientific (lowercase): 3.141590e+00
Scientific (uppercase): 3.141590E+00
%g
or%G
– Automatic format selection:
%g
formats a floating-point value using either decimal or scientific notation, depending on the value’s magnitude.%G
formats a floating-point value using either decimal or scientific notation, with uppercase for the exponent character.
package main
import "fmt"
func main() {
num := 3.14159
fmt.Printf("Automatic (lowercase): %g\n", num)
fmt.Printf("Automatic (uppercase): %G\n", num)
}
Output:
Automatic (lowercase): 3.14159
Automatic (uppercase): 3.14159
%f
,%e
, or%g
with precision specifier:
- You can specify the precision of the float value using the syntax
%.nf
,%.ne
, or%.ng
, wheren
is the desired number of decimal places or significant digits.
package main
import "fmt"
func main() {
num := 3.14159
fmt.Printf("Float (precision 2): %.2f\n", num)
fmt.Printf("Scientific (precision 3): %.3e\n", num)
fmt.Printf("Automatic (precision 4): %.4g\n", num)
}
Output:
Float (precision 2): 3.14
Scientific (precision 3): 3.142e+00
Automatic (precision 4): 3.142
These are some examples of float formatting verbs in Go. You can use these verbs with additional flags and width specifiers to further customize the output format according to your needs.