Use in Go% d,% p,% v etc. placeholder

1, you first need to know what placeholders represent what

These are dead knowledge, the common remember, not commonly used direct look-up table on the line

golang formatting the fmt package implements the I / O functions, similar to the printf and scanf C.

Example types of variables and definitions

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

1) Normal placeholder

Placeholder Explanation For example Export
% v Values ​​respective default format. Printf("%v", people) {Zhangsan}
% + V When printing structure, field names will be added Printf("%+v", people) {Name: zhangsan}
% # V Go representation of the value corresponding syntax Printf("#v", people) main.Human{Name:"zhangsan"}
%T Go corresponding type of value Syntax Notation Printf("%T", people) main.Human
%% Placeholder literal percent sign, the value is not Printf("%%") %

2) Boolean placeholder

Placeholder Explanation For example Export
%t true or false Printf("%t", true) true

3) integer placeholder

Placeholder Explanation For example Export
%b Binary representation Printf("%b", 5) 101
%c Unicode character codes corresponding to the point indicated Printf("%c", 0x4E2D) in
%d Decimal representation Printf("%d", 0x12) 18
%The Octal representation Printf("%d", 10) 12
%q Single quotation marks around the character literal, grammatical safely escaped by Go Printf("%q", 0x4E2D) 'in'
%x Hexadecimal representation, in the form of lower-case letters af Printf("%x", 13) d
%X Hexadecimal representation, in the form of letters to uppercase AF Printf("%x", 13) D
% U Unicode format: U + 1234, equivalent to "U +% 04X" Printf("%U", 0x4E2D) U+4E2D

4) part of the floating point and complex (real and imaginary part)

Placeholder Explanation For example Export
%b No fractional part, the index is a power of two scientific notation consistent with strconv.FormatFloat 'b' of format conversion. E.g. -123456p-78    
%e Scientific notation, e.g. -1234.456e + 78 Printf("%e", 10.2) 1.020000e+01
%E Scientific notation, e.g. -1234.456E + 78 Printf("%e", 10.2) 1.020000E+01
%f There are no decimal point index, for example, 123.456 Printf("%f", 10.2) 10.200000
%g Case of selecting% f% e, or according to produce a more compact (without end 0) output Printf("%g", 10.20) 10.2
%G Case of selecting% E% f, or according to produce a more compact (without end 0) output Printf("%G", 10.20+2i) (10.2 + 2i)

5) byte string sections

Placeholder Explanation For example Export
%s It indicates output string (string or Type [] byte) Printf("%s", []byte("Go语言")) Go Language
%q Double quotes around the string, escaped by Go grammar safely Printf("%q", "Go语言") "Go language"
%x Hexadecimal, lowercase letters, two bytes per character Printf("%x", "golang") 676f6c616e67
%X Hexadecimal, uppercase letters, two bytes per character Printf("%X", "golang") 676F6C616E67

6) pointer

Placeholder Explanation For example Export
%p Hexadecimal notation, the prefix 0x Printf("%p", &people) 0x4f57f0

7)其它标记

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

8)其他

golang没有 '%u' 点位符,若整数为无符号类型,默认就会被打印成无符号的。

宽度与精度的控制格式以Unicode码点为单位。宽度为该数值占用区域的最小宽度;精度为小数点之后的位数。
操作数的类型为int时,宽度与精度都可用字符 '*' 表示。

对于 %g/%G 而言,精度为所有数字的总数,例如:123.45,%.4g 会打印123.5,(而 %6.2f 会打印123.45)。

%e 和 %f 的默认精度为6

对大多数的数值类型而言,宽度为输出的最小字符数,如果必要的话会为已格式化的形式填充空格。

而以字符串类型,精度为输出的最大字符数,如果必要的话会直接截断。

2、了解了基本的占位符对应什么格式就要使用占位符了

Is simple to use, usually with fmt.Printf () to use because of fmt Printf () is the output format, avoid using Println (), otherwise it will output a string.

The main contents are reproduced in the blog garden situation three of the blog

See original: golang fmt format "placeholder"



Author: Juha
link: https: //www.jianshu.com/p/66aaf908045e
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/show58/p/12615055.html