Go语言中的打印格式列表

huangapple go评论91阅读模式
英文:

List of printing format in Go lang

问题

%v 用于打印值。
%T 可以打印值的类型。
还有什么?

英文:

Just want to know the list of printing format using the fmt package's functions.

For example, like:

%v is use for print the value.
%T can print the type of value.

What else?

答案1

得分: 36

The list of format "verbs" is available in the documentation of the fmt package :

General:

%v	默认格式的值。
	当打印结构体时,加号标志(%+v)会添加字段名
%#v	值的Go语法表示
%T	值的类型的Go语法表示
%%	字面百分号;不消耗任何值

布尔值:

%t	true或false

整数:

%b	二进制
%c	由对应的Unicode代码点表示的字符
%d	十进制
%o	八进制
%q	使用Go语法安全转义的单引号字符字面量
%x	十六进制,小写字母a-f
%X	十六进制,大写字母A-F
%U	Unicode格式:U+1234;与"U+%04X"相同

浮点数和复数成分:

%b	无小数点的科学计数法,指数为2的幂,
	类似于strconv.FormatFloat的'b'格式,例如-123456p-78
%e	科学计数法,例如-1234.456e+78
%E	科学计数法,例如-1234.456E+78
%f	有小数点但没有指数,例如123.456
%g	根据%e或%f产生更紧凑的输出
%G	根据%E或%f产生更紧凑的输出

字符串和字节切片:

%s	字符串或切片的未解释字节
%q	使用Go语法安全转义的双引号字符串
%x	十六进制,小写,每个字节两个字符
%X	十六进制,大写,每个字节两个字符

指针:

%p	十六进制表示,带有前导0x

其他标志:

+	始终为数值打印符号;
	对于%q(%+q),保证输出为ASCII字符
-	在右侧填充空格而不是左侧(左对齐字段)
#	备用格式:在八进制(%#o)前添加前导0,十六进制(%#x)前添加0x;
	十六进制(%#X)前添加0X;对于%p(%#p),抑制0x;
	如果可能,对于%q(%#q),打印原始(反引号)字符串;
	对于%U(%#U),如果字符可打印,则写入例如U+0078 'x'。
' '	(空格)在数字中为省略符号留出空格(% d);
	在以十六进制打印字符串或切片时,在字节之间放置空格(% x,% X)
0	用前导零而不是空格填充
英文:

The list of format "verbs" is available in the documentation of the fmt package :

General:

%v	the value in a default format.
	when printing structs, the plus flag (%+v) adds field names
%#v	a Go-syntax representation of the value
%T	a Go-syntax representation of the type of the value
%%	a literal percent sign; consumes no value

Boolean:

%t	the word true or false

Integer:

%b	base 2
%c	the character represented by the corresponding Unicode code point
%d	base 10
%o	base 8
%q	a single-quoted character literal safely escaped with Go syntax.
%x	base 16, with lower-case letters for a-f
%X	base 16, with upper-case letters for A-F
%U	Unicode format: U+1234; same as "U+%04X"

Floating-point and complex constituents:

%b	decimalless scientific notation with exponent a power of two,
	in the manner of strconv.FormatFloat with the 'b' format,
	e.g. -123456p-78
%e	scientific notation, e.g. -1234.456e+78
%E	scientific notation, e.g. -1234.456E+78
%f	decimal point but no exponent, e.g. 123.456
%g	whichever of %e or %f produces more compact output
%G	whichever of %E or %f produces more compact output

String and slice of bytes:

%s	the uninterpreted bytes of the string or slice
%q	a double-quoted string safely escaped with Go syntax
%x	base 16, lower-case, two characters per byte
%X	base 16, upper-case, two characters per byte

Pointer:

%p	base 16 notation, with leading 0x

Other flags:

+	always print a sign for numeric values;
	guarantee ASCII-only output for %q (%+q)
-	pad with spaces on the right rather than the left (left-justify the field)
#	alternate format: add leading 0 for octal (%#o), 0x for hex (%#x);
	0X for hex (%#X); suppress 0x for %p (%#p);
	print a raw (backquoted) string if possible for %q (%#q);
	write e.g. U+0078 'x' if the character is printable for %U (%#U).
' '	(space) leave a space for elided sign in numbers (% d);
	put spaces between bytes printing strings or slices in hex (% x, % X)
0	pad with leading zeros rather than spaces

答案2

得分: 4

你可以了解更多信息,请查看http://golang.org/pkg/fmt/

并运行

$ godoc -http=0.0.0.0:8080

在浏览器中打开localhost:8080以获取完整的离线go网站(包括您的go源代码和文档)

英文:

You can find out more, check this out http://golang.org/pkg/fmt/

and run

$ godoc -http=0.0.0.0:8080

browser open localhost:8080 to get entire offline go website (also including your go src & doc)

huangapple
  • 本文由 发表于 2012年10月8日 20:29:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/12781781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定