总结一下golang中平时遇到过的字符串格式化方法,遇新的到再更新
以结构体为例
type point struct{
x int
y int
}
Printf
%-
package main
import (
"fmt"
)
func main() {
p := point{
x: 1,
y: 2,
}
fmt.Printf("%b\n", 520) //1000001000
fmt.Printf("%o\n", 520) //1010
fmt.Printf("%0x\n", 520) //208
fmt.Printf("%v \n",p) //{1 2}
fmt.Printf("%+v \n",p) //{x:1 y:2}
fmt.Printf("%#v \n",p) //main.point{x:3, y:3}
te()
fmt.Printf("%T\n",p) //main.point{x:3, y:3}
fmt.Printf("%t\n", true)
fmt.Printf("%e\n", 1234000000.0) //1.234000e+09
fmt.Printf("%E\n", 1234000000.0) //1.234000E+09
fmt.Printf("%q\n", "I miss u") //"I miss u"
fmt.Printf("%s\n", "I miss u") //I miss u
fmt.Printf("%p\n", &p) //0xc0000b4010
fmt.Printf("|%6d|%6d|\n", 52, 1314) //| 52| 1314|
fmt.Printf("|%6.2f|%6.2f|\n", 1.2, 3.45) //| 1.20| 3.45|
fmt.Printf("|%-6.2f|%-6.2f|\n", 1.2, 3.45) //|1.20 |3.45 |
}
func te() {
p := point{
x: 3,
y: 3,
}
fmt.Printf("%#v \n",p) //main.point{x:1, y:2}
}
type point struct {
x int
y int
}