32.2 格式化verb应用

在应用上,我们主要讲讲格式化verb ,fmt包中格式化的主要功能函数都在format.go文件中。

我们先来了解下有哪些verb:

符号 含义
指针:
%p 表示为十六进制,并加上前导的0x

宽度通过一个紧跟在百分号后面的十进制数指定,如果未指定宽度,则表示值时除必需之外不作填充。精度通过(可能有的)宽度后跟点号后跟的十进制数指定。如果未指定精度,会使用默认精度;如果点号后没有跟数字,表示精度为0。举例如下:

符号 含义
%f 默认宽度,默认精度
%9f 宽度9,默认精度
%.2f 默认宽度,精度2
%9.2f 宽度9,精度2
%9.f 宽度9,精度0

对于整数,宽度和精度都设置输出总长度。采用精度时表示右对齐并用0填充,而宽度默认表示用空格填充。

对于浮点数,宽度设置输出总长度;精度设置小数部分长度(如果有的话),除了%g/%G,此时精度设置总的数字个数。例如,对数字123.45,格式%6.2f 输出123.45;格式%.4g输出123.5。%e和%f的默认精度是6,%g的默认精度是可以将该值区分出来需要的最小数字个数。

对复数,宽度和精度会分别用于实部和虚部,结果用小括号包裹。因此%f用于1.2+3.4i输出(1.200000+3.400000i)。

其它flag:

符号 含义
+ 总是输出数值的正负号;对%q(%+q)会生成全部是ASCII字符的输出(通过转义);
- 在输出右边填充空白而不是默认的左边(即从默认的右对齐切换为左对齐);
# 切换格式:八进制数前加0(%#o),十六进制数前加0x(%#x)或0X(%#X),指针去掉前面的0x(%#p); 对%q(%#q),如果strconv.CanBackquote返回真会输出反引号括起来的未转义字符串; 对%U(%#U),如果字符是可打印的,会在输出Unicode格式、空格、单引号括起来的Go字面值;
' ' 对数值,正数前加空格而负数前加负号;对字符串采用%x或%X时(% x或% X)会给各打印的字节之间加空格;
0 使用0而不是空格填充,对于数值类型会把填充的0放在正负号后面;

verb会忽略不支持的旗标(flag)。

下面我们用一个程序来演示下:

package main
import (
	"fmt"
	"os"
)
type User struct {
	name string
	age  int
}
var valF float64 = 32.9983
var valI int = 89
var valS string = "Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software."
var valB bool = true
func main() {
	p := User{"John",  28}
	fmt.Printf("Printf struct %%v : %v\n",  p)
	fmt.Printf("Printf struct %%+v : %+v\n",  p)
	fmt.Printf("Printf struct %%#v : %#v\n",  p)
	fmt.Printf("Printf struct %%T : %T\n",  p)
	fmt.Printf("Printf struct %%p : %p\n",  &p)
	fmt.Printf("Printf float64 %%v : %v\n",  valF)
	fmt.Printf("Printf float64 %%+v : %+v\n",  valF)
	fmt.Printf("Printf float64 %%#v : %#v\n",  valF)
	fmt.Printf("Printf float64 %%T : %T\n",  valF)
	fmt.Printf("Printf float64 %%f : %f\n",  valF)
	fmt.Printf("Printf float64 %%4.3f : %4.3f\n",  valF)
	fmt.Printf("Printf float64 %%8.3f : %8.3f\n",  valF)
	fmt.Printf("Printf float64 %%-8.3f : %-8.3f\n",  valF)
	fmt.Printf("Printf float64 %%e : %e\n",  valF)
	fmt.Printf("Printf float64 %%E : %E\n",  valF)
	fmt.Printf("Printf int %%v : %v\n",  valI)
	fmt.Printf("Printf int %%+v : %+v\n",  valI)
	fmt.Printf("Printf int %%#v : %#v\n",  valI)
	fmt.Printf("Printf int %%T : %T\n",  valI)
	fmt.Printf("Printf int %%d : %d\n",  valI)
	fmt.Printf("Printf int %%8d : %8d\n",  valI)
	fmt.Printf("Printf int %%-8d : %-8d\n",  valI)
	fmt.Printf("Printf int %%b : %b\n",  valI)
	fmt.Printf("Printf int %%c : %c\n",  valI)
	fmt.Printf("Printf int %%o : %o\n",  valI)
	fmt.Printf("Printf int %%U : %U\n",  valI)
	fmt.Printf("Printf int %%q : %q\n",  valI)
	fmt.Printf("Printf int %%x : %x\n",  valI)
	fmt.Printf("Printf string %%v : %v\n",  valS)
	fmt.Printf("Printf string %%+v : %+v\n",  valS)
	fmt.Printf("Printf string %%#v : %#v\n",  valS)
	fmt.Printf("Printf string %%T : %T\n",  valS)
	fmt.Printf("Printf string %%x : %x\n",  valS)
	fmt.Printf("Printf string %%X : %X\n",  valS)
	fmt.Printf("Printf string %%s : %s\n",  valS)
	fmt.Printf("Printf string %%200s : %200s\n",  valS)
	fmt.Printf("Printf string %%-200s : %-200s\n",  valS)
	fmt.Printf("Printf string %%q : %q\n",  valS)
	fmt.Printf("Printf bool %%v : %v\n",  valB)
	fmt.Printf("Printf bool %%+v : %+v\n",  valB)
	fmt.Printf("Printf bool %%#v : %#v\n",  valB)
	fmt.Printf("Printf bool %%T : %T\n",  valB)
	fmt.Printf("Printf bool %%t : %t\n",  valB)
	s := fmt.Sprintf("a %s",  "string")
	fmt.Println(s)
	fmt.Fprintf(os.Stderr,  "an %s\n",  "error")
}
程序输出:
Printf struct %v : {John 28}
Printf struct %+v : {name:John age:28}
Printf struct %#v : main.User{name:"John", age:28}
Printf struct %T : main.User
Printf struct %p : 0xc000048400
Printf float64 %v : 32.9983
Printf float64 %+v : 32.9983
Printf float64 %#v : 32.9983
Printf float64 %T : float64
Printf float64 %f : 32.998300
Printf float64 %4.3f : 32.998
Printf float64 %8.3f :   32.998
Printf float64 %-8.3f : 32.998  
Printf float64 %e : 3.299830e+01
Printf float64 %E : 3.299830E+01
Printf int %v : 89
Printf int %+v : 89
Printf int %#v : 89
Printf int %T : int
Printf int %d : 89
Printf int %8d :       89
Printf int %-8d : 89  
Printf int %b : 1011001
Printf int %c : Y
Printf int %o : 131
Printf int %U : U+0059
Printf int %q : 'Y'
Printf int %x : 59
Printf string %v : Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software.
Printf string %+v : Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software.
Printf string %#v : "Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software."
Printf string %T : string
Printf string %x : 476f20697320616e206f70656e20736f757263652070726f6772616d6d696e67206c616e67756167652074686174206d616b6573206974206561737920746f206275696c642073696d706c652c202072656c6961626c652c2020616e6420656666696369656e7420736f6674776172652e
Printf string %X : 476F20697320616E206F70656E20736F757263652070726F6772616D6D696E67206C616E67756167652074686174206D616B6573206974206561737920746F206275696C642073696D706C652C202072656C6961626C652C2020616E6420656666696369656E7420736F6674776172652E
Printf string %s : Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software.
Printf string %200s :                                                                                        Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software.
Printf string %-200s : Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software.                                                                             
Printf string %q : "Go is an open source programming language that makes it easy to build simple,  reliable,  and efficient software."
Printf bool %v : true
Printf bool %+v : true
Printf bool %#v : true
Printf bool %T : bool
Printf bool %t : true
a string
an error

我们主要通过fmt.Printf来理解这些flag 的含义,这对我们今后的开发有较强的实际作用。至于其他函数,我就不一一举例,有兴趣可以进一步研究。