Go-language FMT format "placeholder"

Source: Internet
Author: User

This article is reproduced from http://www.cnblogs.com/qing123/articles/4353353.html?hmsr=studygolang.com&utm_medium= Studygolang.com&utm_source=studygolang.com

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

# 定义示例类型和变量type Human struct {    Name string}var people = Human{Name:"zhangsan"}

Common placeholder

占位符                  说明                      举例                             输出%v              打印一个内置值的值。             Printf("%v", people)          {zhangsan}%+v             打印结构体时,会添加字段名       Printf("%+v", people)         {Name:zhangsan}%#v             相应值的Go语法表示               Printf("#v", people)        main.Human{Name:"zhangsan"}%T              打印内置的或者自定义的类型        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

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

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)

Pointer

占位符         说明                      举例                             输出%p      十六进制表示,前缀 0x          Printf("%p", &people)             0x4f57f0
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.