Imroved string formatting prefixes.

This commit is contained in:
V. Kovpak 2021-09-05 23:08:37 +03:00
parent 8fa7349f92
commit bb4e564ac9
2 changed files with 32 additions and 32 deletions

View File

@ -40,12 +40,20 @@ func main() {
// Use `%d` for standard, base-10 formatting.
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.
fmt.Printf("bin: %b\n", 14)
// This prints the character corresponding to the
// given integer.
fmt.Printf("int2: %c\n", 33)
fmt.Printf("char: %c\n", 33)
// `%x` provides hex encoding.
fmt.Printf("hex: %x\n", 456)
@ -59,6 +67,15 @@ func main() {
fmt.Printf("float2: %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`.
fmt.Printf("str1: %s\n", "\"string\"")
@ -70,40 +87,23 @@ func main() {
// per byte of input.
fmt.Printf("str3: %x\n", "hex this")
// To print a representation of a pointer, use `%p`.
fmt.Printf("pointer: %p\n", &p)
// 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)
// To left-justify use the `-` flag as with numbers.
fmt.Printf("str4: |%-6s|%-6s|\n", "foo", "b")
// You may also want to control width when formatting
// strings, especially to ensure that they align in
// table-like output. For basic right-justified width.
fmt.Printf("str4: |%6s|%6s|\n", "foo", "b")
// To left-justify use the `-` flag as with numbers.
fmt.Printf("str5: |%-6s|%-6s|\n", "foo", "b")
fmt.Printf("str5: |%6s|%6s|\n", "foo", "b")
// So far we've seen `Printf`, which prints the
// formatted string to `os.Stdout`. `Sprintf` formats
// 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)
// To print a representation of a pointer, use `%p`.
fmt.Printf("pointer: %p\n", &p)
// You can format+print to `io.Writers` other than
// `os.Stdout` using `Fprintf`.
fmt.Fprintf(os.Stderr, "io: an %s\n", "error")

View File

@ -5,20 +5,20 @@ struct3: main.point{x:1, y:2}
type: main.point
bool: true
int1: 123
int2: | 12| 345|
bin: 1110
int2: !
char: !
hex: 1c8
float1: 78.900000
float2: 1.234000e+08
float3: 1.234000E+08
float4: |1.20 |3.45 |
float5: | 1.20| 3.45|
str1: "string"
str2: "\"string\""
str3: 6865782074686973
pointer: 0xc420014090
int3: | 12| 345|
float4: | 1.20| 3.45|
float5: |1.20 |3.45 |
str4: |foo |b |
str5: | foo| b|
str6: a string
sprintf: a string
pointer: 0xc420014090
io: an error