Added prefixes with purpose to find output result easier. (#391)

This commit is contained in:
V. K 2021-09-08 15:42:31 +03:00 committed by GitHub
parent 2e6a5ad8c9
commit 33985af4da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 46 deletions

View File

@ -19,59 +19,59 @@ func main() {
// format general Go values. For example, this prints // format general Go values. For example, this prints
// an instance of our `point` struct. // an instance of our `point` struct.
p := point{1, 2} p := point{1, 2}
fmt.Printf("%v\n", p) fmt.Printf("struct1: %v\n", p)
// If the value is a struct, the `%+v` variant will // If the value is a struct, the `%+v` variant will
// include the struct's field names. // include the struct's field names.
fmt.Printf("%+v\n", p) fmt.Printf("struct2: %+v\n", p)
// The `%#v` variant prints a Go syntax representation // The `%#v` variant prints a Go syntax representation
// of the value, i.e. the source code snippet that // of the value, i.e. the source code snippet that
// would produce that value. // would produce that value.
fmt.Printf("%#v\n", p) fmt.Printf("struct3: %#v\n", p)
// To print the type of a value, use `%T`. // To print the type of a value, use `%T`.
fmt.Printf("%T\n", p) fmt.Printf("type: %T\n", p)
// Formatting booleans is straight-forward. // Formatting booleans is straight-forward.
fmt.Printf("%t\n", true) fmt.Printf("bool: %t\n", true)
// 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("%d\n", 123) fmt.Printf("int: %d\n", 123)
// This prints a binary representation. // This prints a binary representation.
fmt.Printf("%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("%c\n", 33) fmt.Printf("char: %c\n", 33)
// `%x` provides hex encoding. // `%x` provides hex encoding.
fmt.Printf("%x\n", 456) fmt.Printf("hex: %x\n", 456)
// There are also several formatting options for // There are also several formatting options for
// floats. For basic decimal formatting use `%f`. // floats. For basic decimal formatting use `%f`.
fmt.Printf("%f\n", 78.9) fmt.Printf("float1: %f\n", 78.9)
// `%e` and `%E` format the float in (slightly // `%e` and `%E` format the float in (slightly
// different versions of) scientific notation. // different versions of) scientific notation.
fmt.Printf("%e\n", 123400000.0) fmt.Printf("float2: %e\n", 123400000.0)
fmt.Printf("%E\n", 123400000.0) fmt.Printf("float3: %E\n", 123400000.0)
// For basic string printing use `%s`. // For basic string printing use `%s`.
fmt.Printf("%s\n", "\"string\"") fmt.Printf("str1: %s\n", "\"string\"")
// To double-quote strings as in Go source, use `%q`. // To double-quote strings as in Go source, use `%q`.
fmt.Printf("%q\n", "\"string\"") fmt.Printf("str2: %q\n", "\"string\"")
// As with integers seen earlier, `%x` renders // As with integers seen earlier, `%x` renders
// the string in base-16, with two output characters // the string in base-16, with two output characters
// per byte of input. // per byte of input.
fmt.Printf("%x\n", "hex this") fmt.Printf("str3: %x\n", "hex this")
// To print a representation of a pointer, use `%p`. // To print a representation of a pointer, use `%p`.
fmt.Printf("%p\n", &p) fmt.Printf("pointer: %p\n", &p)
// When formatting numbers you will often want to // When formatting numbers you will often want to
// control the width and precision of the resulting // control the width and precision of the resulting
@ -79,32 +79,32 @@ 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("|%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("|%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("|%-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("|%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("|%-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("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
// `os.Stdout` using `Fprintf`. // `os.Stdout` using `Fprintf`.
fmt.Fprintf(os.Stderr, "an %s\n", "error") fmt.Fprintf(os.Stderr, "io: an %s\n", "error")
} }

View File

@ -1,24 +1,24 @@
$ go run string-formatting.go $ go run string-formatting.go
{1 2} struct1: {1 2}
{x:1 y:2} struct2: {x:1 y:2}
main.point{x:1, y:2} struct3: main.point{x:1, y:2}
main.point type: main.point
true bool: true
123 int: 123
1110 bin: 1110
! char: !
1c8 hex: 1c8
78.900000 float1: 78.900000
1.234000e+08 float2: 1.234000e+08
1.234000E+08 float3: 1.234000E+08
"string" str1: "string"
"\"string\"" str2: "\"string\""
6865782074686973 str3: 6865782074686973
0x42135100 pointer: 0xc420014090
| 12| 345| width1: | 12| 345|
| 1.20| 3.45| width2: | 1.20| 3.45|
|1.20 |3.45 | float4: |1.20 |3.45 |
| foo| b| width3: | foo| b|
|foo |b | str4: |foo |b |
a string sprintf: a string
an error io: an error