-
Notifications
You must be signed in to change notification settings - Fork 0
/
46-string-formatting.go
61 lines (36 loc) · 973 Bytes
/
46-string-formatting.go
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
//https://gobyexample.com/string-formatting
package main
import (
"fmt"
"os"
)
type point struct {
x, y int
}
func main() {
p := point{1, 2}
fmt.Printf("01-%v\n", p)
fmt.Printf("02-%+v\n", p)
fmt.Printf("03-%#v\n", p)
fmt.Printf("04-%T\n", p)
fmt.Printf("05-%t\n", true)
fmt.Printf("06-%d\n", 123)
fmt.Printf("07-%b\n", 14)
fmt.Printf("08-%c\n", 33)
fmt.Printf("09-%x\n", 456)
fmt.Printf("10-%f\n", 78.9)
fmt.Printf("11-%e\n", 123400000.0)
fmt.Printf("12-%E\n", 123400000.0)
fmt.Printf("13-%s\n", "\"string\"")
fmt.Printf("14-%q\n", "\"string\"")
fmt.Printf("15-%x\n", "hex this")
fmt.Printf("16-%p\n", &p)
fmt.Printf("17-|%6d|%6d|\n", 12, 345)
fmt.Printf("18-|%6.2f|%6.2f|\n", 1.2, 3.45)
fmt.Printf("19-|%-6.2f|%-6.2f|\n", 1.2, 3.45)
fmt.Printf("20-|%6s|%6s|\n", "foo", "b")
fmt.Printf("21-|%-6s|%-6s|\n", "foo", "b")
s := fmt.Sprintf("22-a %s", "string")
fmt.Println(s)
fmt.Fprintf(os.Stderr, "23-an %s\n", "error")
}