"Golang Starter Series two" FMT format "placeholder"

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This paper mainly introduces the FMT placeholder of Golang, which is used frequently and is convenient to consult.

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

Define sample types and variables:

type Human struct {    Name string}var people = Human{Name:"zhangsan"}

Common placeholders:

占位符     说明                           举例                   输出%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 placeholders:

占位符       说明                举例                     输出%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 numbers and complex numbers (real and imaginary parts):

占位符     说明                              举例            输出%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 slices:

占位符     说明                              举例                           输出%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 a '%u ' dot, and 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 point units. The width is the minimum width of the area occupied by the value, and the precision is the number of digits after the decimal point.
When the operand is of type int, the width and precision are indicated by the character ' * '.

For%g/%g, the precision is the total number of all numbers, for example: 123.45,%.4G will print 123.5 (and%6.2f will print 123.45).

The default accuracy of%e and%f is 6

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

In the case of a string type, the precision is the maximum number of characters to output and is truncated if necessary.

In order to facilitate communication, I opened the public number (attention to see more wonderful) and QQ Group, QQ Group 1 (291519319) and QQ Group 2 (659336691). Like the technology to communicate with it

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.