-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path45_string_formatting.go
More file actions
91 lines (66 loc) · 2.1 KB
/
45_string_formatting.go
File metadata and controls
91 lines (66 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package gobyexample
import (
"fmt"
"os"
)
type point struct {
x, y int
}
// StringFormattingDemo - demonstrates Go's string formatting capabilities
func StringFormattingDemo() {
p := point{1, 2}
// %v - print instance of our struct
fmt.Printf("%v\n", p)
// %+v - print instance of our struct with field names
fmt.Printf("%+v\n", p)
// %#v - print Go representation of the value, i.e. the source
// code snippet that would produce that value
fmt.Printf("%#v\n", p)
// %T - print type of value
fmt.Printf("%T\n", p)
// %t - format booleans
fmt.Printf("%t\n", true)
// %d - format integer in base 10
fmt.Printf("%d\n", 123)
// %b - print binary representation
fmt.Printf("%b\n", 14)
// %c - print character corresponding to given integer
fmt.Printf("%c\n", 33)
// %x - print hexadecimal representation
fmt.Printf("%x\n", 456)
// %f - basic decimal formatting
fmt.Printf("%f\n", 78.9)
// %e, %E - scientific notation
fmt.Printf("%e\n", 123400000.0)
fmt.Printf("%E\n", 123400000.0)
// %s - basic string printing
fmt.Printf("%s\n", "\"string\"")
// %q - double-quote strings as in Go source
fmt.Printf("%q\n", "\"string\"")
// %x - render string in base-16, two output chars
// per byte of input
fmt.Printf("%x\n", "hex this")
// %p - print representation of pointer
fmt.Printf("%p\n", &p)
// specify width of integers
// by default result is right-justified and padded with spaces
fmt.Printf("|%6d|%6d|\n", 12, 345)
// specify width of floats
// restrict decimal precision at the same time with
// `width.precision` syntax
fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)
// to left-justify, use the - flag
fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)
// control width when formatting strings
// right-justified by default
fmt.Printf("|%6s|%6s|\n", "foo", "b")
// left-justify with the - flag
fmt.Printf("|%-6s|%-6s|\n", "foo", "b")
// `Sprintf` formats and returns a string without
// printing it anywhere
s := fmt.Sprintf("a %s", "string")
fmt.Println(s)
// format and print to `io.Writers` other than
// `os.Stdout` using `Fprintf`
fmt.Fprintf(os.Stderr, "an %s\n", "error")
}