Imroved string formatting prefixes.
This commit is contained in:
parent
8fa7349f92
commit
bb4e564ac9
@ -40,12 +40,20 @@ func main() {
|
|||||||
// Use `%d` for standard, base-10 formatting.
|
// Use `%d` for standard, base-10 formatting.
|
||||||
fmt.Printf("int1: %d\n", 123)
|
fmt.Printf("int1: %d\n", 123)
|
||||||
|
|
||||||
|
// When formatting numbers you will often want to
|
||||||
|
// control the width and precision of the resulting
|
||||||
|
// figure. To specify the width of an integer, use a
|
||||||
|
// number after the `%` in the verb. By default the
|
||||||
|
// result will be right-justified and padded with
|
||||||
|
// spaces.
|
||||||
|
fmt.Printf("int2: |%6d|%6d|\n", 12, 345)
|
||||||
|
|
||||||
// This prints a binary representation.
|
// This prints a binary representation.
|
||||||
fmt.Printf("bin: %b\n", 14)
|
fmt.Printf("bin: %b\n", 14)
|
||||||
|
|
||||||
// This prints the character corresponding to the
|
// This prints the character corresponding to the
|
||||||
// given integer.
|
// given integer.
|
||||||
fmt.Printf("int2: %c\n", 33)
|
fmt.Printf("char: %c\n", 33)
|
||||||
|
|
||||||
// `%x` provides hex encoding.
|
// `%x` provides hex encoding.
|
||||||
fmt.Printf("hex: %x\n", 456)
|
fmt.Printf("hex: %x\n", 456)
|
||||||
@ -59,6 +67,15 @@ func main() {
|
|||||||
fmt.Printf("float2: %e\n", 123400000.0)
|
fmt.Printf("float2: %e\n", 123400000.0)
|
||||||
fmt.Printf("float3: %E\n", 123400000.0)
|
fmt.Printf("float3: %E\n", 123400000.0)
|
||||||
|
|
||||||
|
// To left-justify, use the `-` flag.
|
||||||
|
fmt.Printf("float4: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
|
||||||
|
|
||||||
|
// You can also specify the width of printed floats,
|
||||||
|
// though usually you'll also want to restrict the
|
||||||
|
// decimal precision at the same time with the
|
||||||
|
// width.precision syntax.
|
||||||
|
fmt.Printf("float5: |%6.2f|%6.2f|\n", 1.2, 3.45)
|
||||||
|
|
||||||
// For basic string printing use `%s`.
|
// For basic string printing use `%s`.
|
||||||
fmt.Printf("str1: %s\n", "\"string\"")
|
fmt.Printf("str1: %s\n", "\"string\"")
|
||||||
|
|
||||||
@ -70,40 +87,23 @@ func main() {
|
|||||||
// per byte of input.
|
// per byte of input.
|
||||||
fmt.Printf("str3: %x\n", "hex this")
|
fmt.Printf("str3: %x\n", "hex this")
|
||||||
|
|
||||||
// To print a representation of a pointer, use `%p`.
|
// To left-justify use the `-` flag as with numbers.
|
||||||
fmt.Printf("pointer: %p\n", &p)
|
fmt.Printf("str4: |%-6s|%-6s|\n", "foo", "b")
|
||||||
|
|
||||||
// When formatting numbers you will often want to
|
|
||||||
// control the width and precision of the resulting
|
|
||||||
// figure. To specify the width of an integer, use a
|
|
||||||
// number after the `%` in the verb. By default the
|
|
||||||
// result will be right-justified and padded with
|
|
||||||
// spaces.
|
|
||||||
fmt.Printf("Int3: |%6d|%6d|\n", 12, 345)
|
|
||||||
|
|
||||||
// You can also specify the width of printed floats,
|
|
||||||
// though usually you'll also want to restrict the
|
|
||||||
// decimal precision at the same time with the
|
|
||||||
// width.precision syntax.
|
|
||||||
fmt.Printf("float4: |%6.2f|%6.2f|\n", 1.2, 3.45)
|
|
||||||
|
|
||||||
// To left-justify, use the `-` flag.
|
|
||||||
fmt.Printf("float5: |%-6.2f|%-6.2f|\n", 1.2, 3.45)
|
|
||||||
|
|
||||||
// You may also want to control width when formatting
|
// You may also want to control width when formatting
|
||||||
// strings, especially to ensure that they align in
|
// strings, especially to ensure that they align in
|
||||||
// table-like output. For basic right-justified width.
|
// table-like output. For basic right-justified width.
|
||||||
fmt.Printf("str4: |%6s|%6s|\n", "foo", "b")
|
fmt.Printf("str5: |%6s|%6s|\n", "foo", "b")
|
||||||
|
|
||||||
// To left-justify use the `-` flag as with numbers.
|
|
||||||
fmt.Printf("str5: |%-6s|%-6s|\n", "foo", "b")
|
|
||||||
|
|
||||||
// So far we've seen `Printf`, which prints the
|
// So far we've seen `Printf`, which prints the
|
||||||
// formatted string to `os.Stdout`. `Sprintf` formats
|
// formatted string to `os.Stdout`. `Sprintf` formats
|
||||||
// and returns a string without printing it anywhere.
|
// and returns a string without printing it anywhere.
|
||||||
s := fmt.Sprintf("str6: a %s", "string")
|
s := fmt.Sprintf("sprintf: a %s", "string")
|
||||||
fmt.Println(s)
|
fmt.Println(s)
|
||||||
|
|
||||||
|
// To print a representation of a pointer, use `%p`.
|
||||||
|
fmt.Printf("pointer: %p\n", &p)
|
||||||
|
|
||||||
// You can format+print to `io.Writers` other than
|
// You can format+print to `io.Writers` other than
|
||||||
// `os.Stdout` using `Fprintf`.
|
// `os.Stdout` using `Fprintf`.
|
||||||
fmt.Fprintf(os.Stderr, "io: an %s\n", "error")
|
fmt.Fprintf(os.Stderr, "io: an %s\n", "error")
|
||||||
|
@ -5,20 +5,20 @@ struct3: main.point{x:1, y:2}
|
|||||||
type: main.point
|
type: main.point
|
||||||
bool: true
|
bool: true
|
||||||
int1: 123
|
int1: 123
|
||||||
|
int2: | 12| 345|
|
||||||
bin: 1110
|
bin: 1110
|
||||||
int2: !
|
char: !
|
||||||
hex: 1c8
|
hex: 1c8
|
||||||
float1: 78.900000
|
float1: 78.900000
|
||||||
float2: 1.234000e+08
|
float2: 1.234000e+08
|
||||||
float3: 1.234000E+08
|
float3: 1.234000E+08
|
||||||
|
float4: |1.20 |3.45 |
|
||||||
|
float5: | 1.20| 3.45|
|
||||||
str1: "string"
|
str1: "string"
|
||||||
str2: "\"string\""
|
str2: "\"string\""
|
||||||
str3: 6865782074686973
|
str3: 6865782074686973
|
||||||
|
str4: |foo |b |
|
||||||
|
str5: | foo| b|
|
||||||
|
sprintf: a string
|
||||||
pointer: 0xc420014090
|
pointer: 0xc420014090
|
||||||
int3: | 12| 345|
|
|
||||||
float4: | 1.20| 3.45|
|
|
||||||
float5: |1.20 |3.45 |
|
|
||||||
str4: | foo| b|
|
|
||||||
str5: |foo |b |
|
|
||||||
str6: a string
|
|
||||||
io: an error
|
io: an error
|
||||||
|
Loading…
x
Reference in New Issue
Block a user