From 49e278a65d2fd606e940ac32eebc4d19745d4a80 Mon Sep 17 00:00:00 2001
From: "V. Kovpak"
package main
point
struct.
p := point{1, 2} - fmt.Printf("%v\n", p) + fmt.Printf("struct1: %v\n", p)@@ -112,7 +112,7 @@ include the struct’s field names.
- fmt.Printf("%+v\n", p) + fmt.Printf("struct2: %+v\n", p)
- fmt.Printf("%#v\n", p) + fmt.Printf("struct3: %#v\n", p)
- fmt.Printf("%T\n", p) + fmt.Printf("type: %T\n", p)
- fmt.Printf("%t\n", true) + fmt.Printf("bool: %t\n", true)
%d
for standard, base-10 formatting.
- fmt.Printf("%d\n", 123) + fmt.Printf("int: %d\n", 123)
%d
for standard, base-10 formatting.
- fmt.Printf("%b\n", 14) + fmt.Printf("bin: %b\n", 14)
- fmt.Printf("%c\n", 33) + fmt.Printf("char: %c\n", 33)
- fmt.Printf("%x\n", 456) + fmt.Printf("hex: %x\n", 456)
%f
.
- fmt.Printf("%f\n", 78.9) + fmt.Printf("float1: %f\n", 78.9)
- fmt.Printf("%e\n", 123400000.0) - fmt.Printf("%E\n", 123400000.0) + fmt.Printf("float2: %e\n", 123400000.0) + fmt.Printf("float3: %E\n", 123400000.0)
- fmt.Printf("%s\n", "\"string\"") + fmt.Printf("str1: %s\n", "\"string\"")
- fmt.Printf("%q\n", "\"string\"") + fmt.Printf("str2: %q\n", "\"string\"")
- fmt.Printf("%x\n", "hex this") + fmt.Printf("str3: %x\n", "hex this")
- fmt.Printf("%p\n", &p) + fmt.Printf("pointer: %p\n", &p)
- fmt.Printf("|%6d|%6d|\n", 12, 345) + fmt.Printf("width1: |%6d|%6d|\n", 12, 345)
- fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45) + fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)
- fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45) + fmt.Printf("float4: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
- fmt.Printf("|%6s|%6s|\n", "foo", "b") + fmt.Printf("width3: |%6s|%6s|\n", "foo", "b")
- fmt.Printf("|%-6s|%-6s|\n", "foo", "b") + fmt.Printf("str4: |%-6s|%-6s|\n", "foo", "b")
- s := fmt.Sprintf("a %s", "string") + s := fmt.Sprintf("sprintf: a %s", "string") fmt.Println(s)
- fmt.Fprintf(os.Stderr, "an %s\n", "error") + fmt.Fprintf(os.Stderr, "io: an %s\n", "error") }
$ go run string-formatting.go -{1 2} -{x:1 y:2} -main.point{x:1, y:2} -main.point -true -123 -1110 -! -1c8 -78.900000 -1.234000e+08 -1.234000E+08 -"string" -"\"string\"" -6865782074686973 -0x42135100 -| 12| 345| -| 1.20| 3.45| -|1.20 |3.45 | -| foo| b| -|foo |b | -a string -an error+struct1: {1 2} +struct2: {x:1 y:2} +struct3: main.point{x:1, y:2} +type: main.point +bool: true +int: 123 +bin: 1110 +char: ! +hex: 1c8 +float1: 78.900000 +float2: 1.234000e+08 +float3: 1.234000E+08 +str1: "string" +str2: "\"string\"" +str3: 6865782074686973 +pointer: 0xc420014090 +width1: | 12| 345| +width2: | 1.20| 3.45| +float4: |1.20 |3.45 | +width3: | foo| b| +str4: |foo |b | +sprintf: a string +io: an error