go语言为printf
传统中的字符串格式化提供了极好的支持。 以下是常见字符串格式化任务的一些示例。
go提供了几种打印“动词”,设计用于格式化一般的值。 例如,打印 point
结构的一个实例。
如果值是一个结构体,%+v
变体将包括结构体的字段名。
%#v
变体打印值的go语法表示,即将生成该值的源代码片段。
要打印值的类型,请使用 %t
。格式化布尔是比较直截了当的。有许多格式化整数的选项。对于标准的base-10
格式化,请使用%d
。
具体的每个函数,可参考示例中的代码 -
所有的示例代码,都放在
f:\worksp\golang
目录下。安装go编程环境请参考:http://www.h3.com/go/go_environment.html
string-formatting.go
的完整代码如下所示 -
package main
import "fmt"
import "os"
type point struct {
x, y int
}
func main() {
// go offers several printing "verbs" designed to
// format general go values. for example, this prints
// an instance of our `point` struct.
p := point{1, 2}
fmt.printf("%v\n", p)
// if the value is a struct, the `%+v` variant will
// include the struct's field names.
fmt.printf("%+v\n", p)
// the `%#v` variant prints a go syntax representation
// of the value, i.e. the source code snippet that
// would produce that value.
fmt.printf("%#v\n", p)
// to print the type of a value, use `%t`.
fmt.printf("%t\n", p)
// formatting booleans is straight-forward.
fmt.printf("%t\n", true)
// there are many options for formatting integers.
// use `%d` for standard, base-10 formatting.
fmt.printf("%d\n", 123)
// this prints a binary representation.
fmt.printf("%b\n", 14)
// this prints the character corresponding to the
// given integer.
fmt.printf("%c\n", 33)
// `%x` provides hex encoding.
fmt.printf("%x\n", 456)
// there are also several formatting options for
// floats. for basic decimal formatting use `%f`.
fmt.printf("%f\n", 78.9)
// `%e` and `%e` format the float in (slightly
// different versions of) scientific notation.
fmt.printf("%e\n", 123400000.0)
fmt.printf("%e\n", 123400000.0)
// for basic string printing use `%s`.
fmt.printf("%s\n", "\"string\"")
// to double-quote strings as in go source, use `%q`.
fmt.printf("%q\n", "\"string\"")
// as with integers seen earlier, `%x` renders
// the string in base-16, with two output characters
// per byte of input.
fmt.printf("%x\n", "hex this")
// to print a representation of a pointer, use `%p`.
fmt.printf("%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("|%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("|%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 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("|%6s|%6s|\n", "foo", "b")
// to left-justify use the `-` flag as with numbers.
fmt.printf("|%-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("a %s", "string")
fmt.println(s)
// you can format+print to `io.writers` other than
// `os.stdout` using `fprintf`.
fmt.fprintf(os.stderr, "an %s\n", "error")
}
执行上面代码,将得到以下输出结果 -
f:\worksp\golang>go run string-formatting.go
{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
0xc042004280
| 12| 345|
| 1.20| 3.45|
|1.20 |3.45 |
| foo| b|
|foo |b |
a string
an error