這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
原文
基本
%vthe value in a default formatwhen printing structs, the plus flag (%+v) adds field names%#va Go-syntax representation of the value%Ta Go-syntax representation of the type of the value%%a literal percent sign; consumes no value
The default format for %v is:
bool: %tint, int8 etc.: %duint, uint8 etc.: %d, %#x if printed with %#vfloat32, complex64, etc: %gstring: %schan: %ppointer: %p
Boolean:
%tthe word true or false
Integer:
%bbase 2%cthe character represented by the corresponding Unicode code point%dbase 10%obase 8%qa single-quoted character literal safely escaped with Go syntax.%xbase 16, with lower-case letters for a-f%Xbase 16, with upper-case letters for A-F%UUnicode format: U+1234; same as "U+%04X"
Floating-point and complex constituents:
%bdecimalless scientific notation with exponent a power of two,in the manner of strconv.FormatFloat with the 'b' format,e.g. -123456p-78%escientific notation, e.g. -1.234456e+78%Escientific notation, e.g. -1.234456E+78%fdecimal point but no exponent, e.g. 123.456%Fsynonym for %f%g%e for large exponents, %f otherwise%G%E for large exponents, %F otherwise
String and slice of bytes (treated equivalently with these verbs):
%sthe uninterpreted bytes of the string or slice%qa double-quoted string safely escaped with Go syntax%xbase 16, lower-case, two characters per byte%Xbase 16, upper-case, two characters per byte
Pointer:
%pbase 16 notation, with leading 0x