Go格式化占位符汇总

1:占位符 placeholder

1.1 通用占位符 placeholder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// %v: 值的默认格式表示
fmt.Printf("%v\n", 100) //100

// %+v: 类似%v,但输出结构体时会添加字段名
o := struct{ name string }{"tim"}
fmt.Printf("%+v\n", o)  //{name:tim}

// %#v: 值的 Go 语法表示
o := struct{ name string }{"tim"}
fmt.Printf("%#v\n", o)  //struct { name string }{name:"tim"}

// %T: 打印值的类型
o := struct{ name string }{"tim"}
fmt.Printf("%T\n", o) //struct { name string }

// %%: 打印%百分号
fmt.Printf("100%%\n")  //100%

// %t: 布尔型 boolean(true,flase)
t := true
fmt.Printf("value: %t", t)

1.2 整形占位符 placeholder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// %b 整形二进制
fmt.Printf("%b\n", 65)//1000001

// %c 该值对应的unicode码值
fmt.Printf("%c\n", 65)//A

// %d 表示为十进制
fmt.Printf("%d\n", 65)//65

// %o 表示为八进制
fmt.Printf("%o\n", 65)//101

// %x 表示为十六进制,使用1-f
fmt.Printf("%x\n", 65)//41

// %X 表示为十六进制,使用A-F
fmt.Printf("%X\n", 65)//41

// %U 表示为Unicode格式:U+1234,等价于”U+%04X”
fmt.Printf("%U\n", 65) // U+0041

// %q 该值对应的单引号括起来的go语法字符字面值必要时会采用安全的转义表示
fmt.Printf("%q\n", 65)//'A'

1.3 浮点数与复数 placeholder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
f := 12.34
// 浮点型,有小数部分但无指数部分
fmt.Printf("%f\n", f) // 12.340000
// 小数点后2位小数
fmt.Printf("%.2f\n", f) // 12.34
fmt.Printf("%F\n", f)   // 12.340000
// 无小数部分、二进制指数的科学计数法
fmt.Printf("%b\n", f) // 6946802425218990p-49
// 科学计数法
fmt.Printf("%e\n", f) // 1.234000e+01
// 根据实际情况采用%e或%f格式
fmt.Printf("%g\n", f) // 12.34
// 根据实际情况采用%E或%F格式
fmt.Printf("%G\n", f) // 12.34

1.4 字符串和[]byte placeholder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// %s  直接输出字符串或者[]byte
fmt.Printf("%s\n", "tim") //tim

// %q 该值对应的双引号括起来的go语法字符串字面值,必要时会采用安全的转义表示
fmt.Printf("%q\n", "tim") //"tim"

// %x 每个字节用两字符十六进制数表示(使用1-f)
fmt.Printf("%x\n", "tim") //74696d

// %X 每个字节用两字符十六进制数表示(使用1-F)
fmt.Printf("%X\n", "tim") //74696D

1.5 pointer

1
2
3
4
// %p 表示为十六进制,并加上前导的0x
t := []int{1}
fmt.Printf("value: %p", t)
// value: 0x14000190008

1.6 宽度标识符 placeholder

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// %f 默认宽度,默认精度
v := 92873.2309837
fmt.Printf("value: %f\n", v) // value: 92873.230984

// %9f 宽度9,默认精度
fmt.Printf("value: %9f\n", v) // value: 92873.230984

// %9.f  宽度9,精度0
fmt.Printf("value: %9.f\n", v) // value:     92873

// %9.2f 宽度9,精度2
fmt.Printf("value: %9.2f\n", v) // value:  92873.23

1.7 其他 flag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
'+'
总是输出数值的正负号%q%+q会生成全部是ASCII字符的输出通过转义
v := 92873.2309837
fmt.Printf("value: %+f", v) // value: +92873.230984

' '
对数值正数前加空格而负数前加负号对字符串采用%x或%X时% x或% X会给各打印的字节之间加空格
v := 92873.2309837
fmt.Printf("value: %+f\n", v) // value: +92873.230984
fmt.Printf("value: % f\n", v) // value:  92873.230984

'0'
使用0而不是空格填充对于数值类型会把填充的0放在正负号后面
fmt.Printf("value: %07.2f\n", v) // value: 92873.23

'#'
八进制数前加0%#o),十六进制数前加0x%#x或0X%#X),指针去掉前面的0x%#p%q%#q),%U%#U会输出空格和单引号括起来的go字面值

'-'
在输出右边填充空白而不是默认的左边即从默认的右对齐切换为左对齐
v := 101.35345345
fmt.Printf("value: %-3.1f\n", v) // value: 101.4

2:go 占位符

通用占位符 placeholder

bool

整型

浮点型与复数

字符串和[]byte

指针 Pointer

宽度(精度)标识符

其他falg