String Formatting {.en}

字符串格式 {.zh}

::: {.en} Go offers excellent support for string formatting in the printf tradition. Here are some examples of common string formatting tasks. :::

::: {.zh}

Go为printf传统中的字符串格式提供了出色的支持。以下是常见字符串格式化任务的一些示例。

:::

  1. package main
  2. import "fmt"
  3. import "os"
  4. type point struct {
  5. x, y int
  6. }
  7. func main() {

::: {.en} Go offers several printing “verbs” designed to format general Go values. For example, this prints an instance of our point struct. :::

::: {.zh}

Go提供了几个印刷“动词”设计toformat一般Go值。例如,这打印了我们的point结构的实例。

:::

  1. p := point{1, 2}
  2. fmt.Printf("%vn", p)

::: {.en} If the value is a struct, the %+v variant will include the struct’s field names. :::

::: {.zh}

如果值是结构,则%+ v变量将包含结构的字段名称。

:::

  1. fmt.Printf("%+vn", p)

::: {.en} The %#v variant prints a Go syntax representation of the value, i.e. the source code snippet that would produce that value. :::

::: {.zh}

%#v变体打印值的i语法表示。将生成该值的源代码段。

:::

  1. fmt.Printf("%#vn", p)

::: {.en} To print the type of a value, use %T. :::

::: {.zh}

要打印值的类型,请使用%T

:::

  1. fmt.Printf("%Tn", p)

::: {.en} Formatting booleans is straight-forward. :::

::: {.zh}

格式化布尔值是直截了当的。

:::

  1. fmt.Printf("%tn", true)

::: {.en} There are many options for formatting integers. Use %d for standard, base-10 formatting. :::

::: {.zh}

格式化整数有很多选项。使用%d作为标准的基础10格式。

:::

  1. fmt.Printf("%dn", 123)

::: {.en} This prints a binary representation. :::

::: {.zh}

这将打印二进制表示。

:::

  1. fmt.Printf("%bn", 14)

::: {.en} This prints the character corresponding to the given integer. :::

::: {.zh}

这将打印与给定整数对应的字符。

:::

  1. fmt.Printf("%cn", 33)

::: {.en} %x provides hex encoding. :::

::: {.zh}

%x提供十六进制编码。

:::

  1. fmt.Printf("%xn", 456)

::: {.en} There are also several formatting options for floats. For basic decimal formatting use %f. :::

::: {.zh}

浮动还有几种格式化选项。对于基本的十进制格式,请使用%f

:::

  1. fmt.Printf("%fn", 78.9)

::: {.en} %e and %E format the float in (slightly different versions of) scientific notation. :::

::: {.zh}

%e%E格式化浮点数(略有不同版本)的科学记数法。

:::

  1. fmt.Printf("%en", 123400000.0)
  2. fmt.Printf("%En", 123400000.0)

::: {.en} For basic string printing use %s. :::

::: {.zh}

对于基本字符串打印,请使用%s

:::

  1. fmt.Printf("%sn", ""string"")

::: {.en} To double-quote strings as in Go source, use %q. :::

::: {.zh}

要像Go源一样双引号字符串,请使用%q

:::

  1. fmt.Printf("%qn", ""string"")

::: {.en} As with integers seen earlier, %x renders the string in base-16, with two output characters per byte of input. :::

::: {.zh}

与前面看到的整数一样,%x在base-16中呈现字符串,两个输出字符为输入字节。

:::

  1. fmt.Printf("%xn", "hex this")

::: {.en} To print a representation of a pointer, use %p. :::

::: {.zh}

要打印指针的表示,请使用%p

:::

  1. fmt.Printf("%pn", &p)

::: {.en} 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. :::

::: {.zh}

格式化数字时,您通常需要控制生成图形的宽度和精度。要指定整数的宽度,请在动词中的之后使用数字。默认情况下,结果将是右对齐的并用空格填充。

:::

  1. fmt.Printf("|%6d|%6d|n", 12, 345)

::: {.en} 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. :::

::: {.zh}

您也可以指定打印浮动的宽度,但通常您也希望使用width.precision语法同时限制十进制精度。

:::

  1. fmt.Printf("|%6.2f|%6.2f|n", 1.2, 3.45)

::: {.en} To left-justify, use the - flag. :::

::: {.zh}

要左对齐,请使用-标志。

:::

  1. fmt.Printf("|%-6.2f|%-6.2f|n", 1.2, 3.45)

::: {.en} 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. :::

::: {.zh}

您可能还希望在格式化字符串时控制宽度,尤其是确保它们对齐类似于intable的输出。对于基本右对齐宽度。

:::

  1. fmt.Printf("|%6s|%6s|n", "foo", "b")

::: {.en} To left-justify use the - flag as with numbers. :::

::: {.zh}

左对齐使用-标志和数字一样。

:::

  1. fmt.Printf("|%-6s|%-6s|n", "foo", "b")

::: {.en} So far we’ve seen Printf, which prints the formatted string to os.Stdout. Sprintf formats and returns a string without printing it anywhere. :::

::: {.zh}

到目前为止,我们已经看过Printf,它将格式化的字符串打印到os.StdoutSprintf格式并返回一个字符串而不在任何地方打印它。

:::

  1. s := fmt.Sprintf("a %s", "string")
  2. fmt.Println(s)

::: {.en} You can format+print to io.Writers other than os.Stdout using Fprintf. :::

::: {.zh}

您可以使用Fprintf格式化+打印到o.S.out以外的io.Writers

:::

  1. fmt.Fprintf(os.Stderr, "an %sn", "error")
  2. }
  1. $ go run string-formatting.go
  2. {1 2}
  3. {x:1 y:2}
  4. main.point{x:1, y:2}
  5. main.point
  6. true
  7. 123
  8. 1110
  9. !
  10. 1c8
  11. 78.900000
  12. 1.234000e+08
  13. 1.234000E+08
  14. "string"
  15. ""string""
  16. 6865782074686973
  17. 0x42135100
  18. | 12| 345|
  19. | 1.20| 3.45|
  20. |1.20 |3.45 |
  21. | foo| b|
  22. |foo |b |
  23. a string
  24. an error