"Golang Introduction Series II" fmt format "placeholder"

Preface: This article mainly introduces Golang's fmt placeholder, which is often used when using it for easy reference

 

Golang's fmt package implements formatted I/O functions, similar to C's printf and scanf.

Define example types and variables:

type Human struct {
    Name string
}

var people = Human{Name:"zhangsan"}

Normal placeholder:

占位符     说明                           举例                   输出
%v      相应值的默认格式。            Printf("%v", people)   {zhangsan},
%+v     打印结构体时,会添加字段名     Printf("%+v", people)  {Name:zhangsan}
%#v     相应值的Go语法表示            Printf("#v", people)   main.Human{Name:"zhangsan"}
%T      相应值的类型的Go语法表示       Printf("%T", people)   main.Human
%%      字面上的百分号,并非值的占位符  Printf("%%")            %

Boolean placeholder:

占位符       说明                举例                     输出
%t          true 或 false。     Printf("%t", true)       true

Integer placeholders:

占位符     说明                                  举例                       输出
%b      二进制表示                             Printf("%b", 5)             101
%c      相应Unicode码点所表示的字符              Printf("%c", 0x4E2D)        中
%d      十进制表示                             Printf("%d", 0x12)          18
%o      八进制表示                             Printf("%d", 10)            12
%q      单引号围绕的字符字面值,由Go语法安全地转义  Printf("%q", 0x4E2D)        '中'
%x      十六进制表示,字母形式为小写 a-f          Printf("%x", 13)             d
%X      十六进制表示,字母形式为大写 A-F          Printf("%x", 13)             D
%U      Unicode格式:U+1234,等同于 "U+%04X"   Printf("%U", 0x4E2D)         U+4E2D

Components of floating-point and complex numbers (real and imaginary):

占位符     说明                              举例            输出
%b      无小数部分的,指数为二的幂的科学计数法,
        与 strconv.FormatFloat 的 'b' 转换格式一致。例如 -123456p-78
%e      科学计数法,例如 -1234.456e+78        Printf("%e", 10.2)     1.020000e+01
%E      科学计数法,例如 -1234.456E+78        Printf("%e", 10.2)     1.020000E+01
%f      有小数点而无指数,例如 123.456        Printf("%f", 10.2)     10.200000
%g      根据情况选择 %e 或 %f 以产生更紧凑的(无末尾的0)输出 Printf("%g", 10.20)   10.2
%G      根据情况选择 %E 或 %f 以产生更紧凑的(无末尾的0)输出 Printf("%G", 10.20+2i) (10.2+2i)

String and byte slice:

占位符     说明                              举例                           输出
%s      输出字符串表示(string类型或[]byte)   Printf("%s", []byte("Go语言"))  Go语言
%q      双引号围绕的字符串,由Go语法安全地转义  Printf("%q", "Go语言")         "Go语言"
%x      十六进制,小写字母,每字节两个字符      Printf("%x", "golang")         676f6c616e67
%X      十六进制,大写字母,每字节两个字符      Printf("%X", "golang")         676F6C616E67

pointer:

占位符         说明                      举例                             输出
%p      十六进制表示,前缀 0x          Printf("%p", &people)             0x4f57f0

Other tags:

占位符      说明                             举例          输出
+      总打印数值的正负号;对于%q(%+q)保证只输出ASCII编码的字符。 
                                           Printf("%+q", "中文")  "\u4e2d\u6587"
-      在右侧而非左侧填充空格(左对齐该区域)
#      备用格式:为八进制添加前导 0(%#o)      Printf("%#U", '中')      U+4E2D
       为十六进制添加前导 0x(%#x)或 0X(%#X),为 %p(%#p)去掉前导 0x;
       如果可能的话,%q(%#q)会打印原始 (即反引号围绕的)字符串;
       如果是可打印字符,%U(%#U)会写出该字符的
       Unicode 编码形式(如字符 x 会被打印成 U+0078 'x')。
' '    (空格)为数值中省略的正负号留出空白(% d);
       以十六进制(% x, % X)打印字符串或切片时,在字节之间用空格隔开
0      填充前导的0而非空格;对于数字,这会将填充移到正负号之后

golang does not have the '%u' dot character, if the integer is an unsigned type, it will be printed as unsigned by default.

 

The control format for width and precision is in Unicode code points. The width is the minimum width of the area occupied by the value; the precision is the number of digits after the decimal point.
When the type of the operand is int, the width and precision can be represented by the character '*'.

For %g/%G, the precision is the sum of all numbers, eg: 123.45, %.4g would print 123.5, (and %6.2f would print 123.45).

The default precision of %e and %f is 6

For most numeric types, the width is the minimum number of characters to output, with spaces padded if necessary for the formatted form.

In the case of string type, the precision is the maximum number of characters in the output, and if necessary, it will be truncated directly.

In order to facilitate everyone's communication, I have opened the WeChat public account (follow for more exciting) and QQ group, QQ group 1 (291519319) and QQ group 2 (659336691). If you like technology, let's communicate

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325260724&siteId=291194637