gobyexample/formatting/formatting.go

38 lines
782 B
Go
Raw Normal View History

2024-05-01 14:42:00 +00:00
package formatting
import (
"fmt"
"math"
)
var f = fmt.Printf
type Person struct {
name string
age uint
}
func Formatting() {
p := Person{"Nadeshiko", 15}
f("int: %d\n", 5)
f("float: %f\n", 4)
f("float (scientific): %e\n", math.Pow(2, 32))
f("string: %s\n", "hello")
f("go value loose: %v\n", p)
f("go value strict: %+v\n", p)
f("go value verbose: %#v\n", p)
f("go bool: %t\n", true)
f("go type: %T\n", p)
f("go ptr address: %p\n", &p)
f("go hex string: %x\n", "foo")
f("go hex int: %x\n", 16)
f("go bin int: %b\n", 5)
f("go oct int: %o\n", 8)
f("go num width: |%5d|\n", 2)
f("go num width (left align): |%-5d|\n", 2)
f("go float precision (left align): |%-5.2f|\n", 3.141516)
f("go char: %c\n", 97)
2024-05-01 14:46:16 +00:00
f("go custom width specifier: |%*s|\n", 5, "foo")
2024-05-01 14:42:00 +00:00
}