Imroved string formatting prefixes.

This commit is contained in:
V. Kovpak 2021-09-08 09:54:58 +03:00
parent c3b7425224
commit d2f31aca0b
2 changed files with 16 additions and 16 deletions

View File

@ -38,14 +38,14 @@ func main() {
// There are many options for formatting integers. // There are many options for formatting integers.
// Use `%d` for standard, base-10 formatting. // Use `%d` for standard, base-10 formatting.
fmt.Printf("int1: %d\n", 123) fmt.Printf("int: %d\n", 123)
// 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)
@ -79,29 +79,29 @@ func main() {
// number after the `%` in the verb. By default the // number after the `%` in the verb. By default the
// result will be right-justified and padded with // result will be right-justified and padded with
// spaces. // spaces.
fmt.Printf("Int3: |%6d|%6d|\n", 12, 345) fmt.Printf("width1: |%6d|%6d|\n", 12, 345)
// You can also specify the width of printed floats, // You can also specify the width of printed floats,
// though usually you'll also want to restrict the // though usually you'll also want to restrict the
// decimal precision at the same time with the // decimal precision at the same time with the
// width.precision syntax. // width.precision syntax.
fmt.Printf("float4: |%6.2f|%6.2f|\n", 1.2, 3.45) fmt.Printf("width2: |%6.2f|%6.2f|\n", 1.2, 3.45)
// To left-justify, use the `-` flag. // To left-justify, use the `-` flag.
fmt.Printf("float5: |%-6.2f|%-6.2f|\n", 1.2, 3.45) fmt.Printf("float4: |%-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("width3: |%6s|%6s|\n", "foo", "b")
// To left-justify use the `-` flag as with numbers. // To left-justify use the `-` flag as with numbers.
fmt.Printf("str5: |%-6s|%-6s|\n", "foo", "b") fmt.Printf("str4: |%-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)
// You can format+print to `io.Writers` other than // You can format+print to `io.Writers` other than

View File

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