Example code for Go string formatting

  • 2020-10-23 20:07:43
  • OfStack

Go has good support for string formatting. Let's look at some common examples of string formatting.


package main
import "fmt"
import "os"
type point struct {
  x, y int
}
func main() {
  // Go Several print formats are provided for formatting 1 a Go Value, such as 
  //  The following %v Print the 1 a point The value of the object of the structure 
  p := point{1, 2}
  fmt.Printf("%v\n", p)
  //  If the formatted value is 1 Struct object, so `%+v` Formatted output of 
  //  The member name and value of the structure will be included 
  fmt.Printf("%+v\n", p)
  // `%#v` Formatted output will output 1 A value of Go Syntax representation. 
  fmt.Printf("%#v\n", p)
  //  use `%T` To output 1 The data type of the value 
  fmt.Printf("%T\n", p)
  //  Format Boolean variables 
  fmt.Printf("%t\n", true)
  //  There are many ways to format an integer, using `%d` is 1 Kind of 
  //  The standard to 10 The way to output an integer in a base system 
  fmt.Printf("%d\n", 123)
  //  This way the output integer type 2 Base representation 
  fmt.Printf("%b\n", 14)
  //  The character corresponding to the integer value is printed here 
  fmt.Printf("%c\n", 33)
  //  use `%x` The output 1 A value of 16 Base representation 
  fmt.Printf("%x\n", 456)
  //  Floating point values can also be formatted in several ways. The most basic 1 One is `%f`
  fmt.Printf("%f\n", 78.9)
  // `%e` and `%E` Use scientific counting to output integers 
  fmt.Printf("%e\n", 123400000.0)
  fmt.Printf("%E\n", 123400000.0)
  //  use `%s` Output the basic string 
  fmt.Printf("%s\n", "\"string\"")
  //  Output like Go A string with double quotes as in the source code should be used `%q`
  fmt.Printf("%q\n", "\"string\"")
  // `%x` In order to 16 Base output string, byte of each string output in two characters 
  fmt.Printf("%x\n", "hex this")
  //  use `%p` The output 1 The value of a pointer 
  fmt.Printf("%p\n", &p)
  //  When you output Numbers, you often need to control the width and precision of the output. 
  //  You can use 1 A is located in the % After the number to control the output width, default 
  //  In this case the output is right-justified and the left is blank 
  fmt.Printf("|%6d|%6d|\n", 12, 345)
  //  You can also specify the output width of a floating-point number, and you can also specify a floating-point number 
  //  Output accuracy 
  fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45)
  // To left-justify, use the `-` flag.
  fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45)
  //  You can also specify the width of the output strings to ensure that they are output aligned. The default 
  //  In this case, the output is right-aligned 
  fmt.Printf("|%6s|%6s|\n", "foo", "b")
  //  To use left alignment you can put it before the width `-` No. 
  fmt.Printf("|%-6s|%-6s|\n", "foo", "b")
  // `Printf` The output of the function is output to the command line `os.Stdout` And you 
  //  You can use `Sprintf` To assign the formatted string to 1 A variable 
  s := fmt.Sprintf("a %s", "string")
  fmt.Println(s)
  //  You can use it, too `Fprintf` To output the formatted value to `io.Writers`
  fmt.Fprintf(os.Stderr, "an %s\n", "error")
}

The results

[

{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
0xc000092000
| 12| 345|
| 1.20| 3.45|
|1.20 |3.45 |
| foo| b|
|foo |b |
a string
an error

]

conclusion

Above is the site to introduce Go string formatting example code details, I hope to help you!


Related articles: