只想知道使用fmt软件包功能的打印格式列表.

例如,如下所示:

%v用于打印值. %T可以打印值的类型.

还有什么?

推荐答案

格式"动词"列表在以下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

Go相关问答推荐

如何修复Go中调用GetRawInputDeviceInfA Windows API函数时的错误?

如何使用工作区方法扩展克隆的Golang库

如何模拟go的Elastic search SDK?

如何确定泛型类型在运行时是否可比较?

golang testscript .txtar 语法,用于 stderr 或 stdout 中包含的文本

无效操作:v > max(类型参数 T 与 > 不可比较)

Go 中的 protobuf FieldMask 解组

Go struct 匿名字段是公开的还是私有的?

如何在 `hashicorp / terraform-exec` 中将 `ApplyConfig` 传递给 `tf.Apply()`?

缺少签名帮助文档

使用 golang 生成 vim

如何模仿联合类型

GRPC 元数据未在 Go 中更新

有没有办法将 yaml node 添加到 golang 中现有的 yaml 文档中?

转到文本/模板模板:如何根据模板本身的值数组判断值?

分配空切片而不引用其类型?

是否可以使用按位运算在随机 unicode 字符串中找到重复字符?

gqlgen go,通过添加一个解析器来减少数据库调用

如何在 Unmarshal 中使用泛型(转到 1.18)

有没有一种方法可以确保传递的值具有使用泛型的某些字段?