Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save guomaimang/a8cca69b03ba750721c005f4ddf15937 to your computer and use it in GitHub Desktop.
Save guomaimang/a8cca69b03ba750721c005f4ddf15937 to your computer and use it in GitHub Desktop.
在go中的 format print,%w 和 %v d的区别是什么

在 Go 中,fmt 包中的格式化输出有很多的选项,其中包括 %w%v 以及 %d。它们的区别如下:

  • %w:用于格式化错误类型(error),会将其转换为字符串并写入输出流。如果错误不为空,则输出错误信息,否则不输出任何内容。这个选项通常用于错误处理的日志输出,以便在记录错误信息的同时也将其输出到日志中。
  • %v:用于格式化任何值类型,包括基本类型和结构体。它会根据值的类型进行格式化输出,并且会自动递归处理结构体和嵌套类型。这个选项通常用于打印调试信息或日志。
  • %d:用于格式化整数类型,包括有符号整数和无符号整数。它会将整数格式化为十进制表示,并将其写入输出流。这个选项通常用于打印调试信息或日志。

因此,%w 用于格式化错误,%v 用于格式化任意值类型,而 %d 用于格式化整数类型。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment