fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf. 格式“占位符”衍生自C,但比C更简单。
type Human struct {
Name string
}
var people = Human{Name:"tom"}
fmt.Printf("%v", people) // {tom}
fmt.Printf("%+v", people) // {Name:tom}
fmt.Printf("#v", people) // main.Human{Name:"tom"}
fmt.Printf("%T", people) // main.Human
fmt.Printf("%%") // Printf("%%")
代码
fmt.Printf("%t", true) // true
Printf("%b", 5) // 101
Printf("%c", 0x4E2D) // 中
Printf("%d", 0x12) //18
Printf("%d", 10) //12
Printf("%q", 0x4E2D) //'中'
Printf("%x", 13) //d
Printf("%X", 13) //D
Printf("%U", 0x4E2D) //U+4E2D
Printf("%e", 10.2) //1.020000e+01
Printf("%e", 10.2) //1.020000E+01
Printf("%f", 10.2) //10.200000
Printf("%g", 10.20) //10.2
Printf("%G", 10.20+2i) //(10.2+2i)
Printf("%s", []byte("Go")) // Go
Printf("%q", "Go") // "Go"
Printf("%x", "golang") // 676f6c616e67
Printf("%X", "golang") // 676F6C616E67
Printf("%p", &people) // 0x4f57f0