前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang 库 - 格式化IO

Golang 库 - 格式化IO

作者头像
twowinter
发布2020-04-17 17:35:17
5390
发布2020-04-17 17:35:17
举报
文章被收录于专栏:twowintertwowinter

1 格式化IO库

Package fmt implements formatted I/O with functions analogous to C’s printf and scanf. The format ‘verbs’ are derived from C’s but are simpler.

fmt 包用于 格式化IO,类似于C语言的 printf 和 scanf。其占位符源自于C语言,但更加简单。

格式化打印的说明

代码语言:javascript
复制
func Print(v ...interface{})
func Printf(format string, v ...interface{})
func Println(v ...interface{})

有且仅在 [f] 结尾的打印接口中,f 表示 format,才可以进行格式化打印。此时占位符才会发生作用。

2 占位符通用说明

代码语言:javascript
复制
%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

%v,打印变量的具体数值。万能打印,会根据变量的类型做调整。 %T,打印变量的类型。

3 不同数据类型的数值打印

其实统一用 %v 就很省事了。

代码语言:javascript
复制
bool:                    %t 
int, int8 etc.:          %d 
uint, uint8 etc.:        %d, %x if printed with %#v
float32, complex64, etc: %g
string:                  %s
chan:                    %p 
pointer:                 %p

复杂数据类型的格式化是这样:

代码语言:javascript
复制
struct:            {field0 field1 ...} 
array, slice:      [elem0 elem1 ...] 
maps:              map[key1:value1 key2:value2] 
pointer to above:  &{}, &[], &map[]

4 其他标记

代码语言:javascript
复制
+	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 0b for binary (%#b), 0 for octal (%#o),
	0x or 0X for hex (%#x or %#X); suppress 0x for %p (%#p);
	for %q, print a raw (backquoted) string if strconv.CanBackquote
	returns true;
	always print a decimal point for %e, %E, %f, %F, %g and %G;
	do not remove trailing zeros for %g and %G;
	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;
	for numbers, this moves the padding after the sign

看下来应该应该是空格填充比较有用,做个小测试。

代码语言:javascript
复制
	u1 := []byte {0x31, 0x32}
	log.Printf("0x:%x\n", u1)
    log.Printf("0x:% x\n", u1)

输出

代码语言:javascript
复制
0x:3132
0x:31 32

5 格式化错误

代码语言:javascript
复制
Wrong type or unknown verb: %!verb(type=value)
	Printf("%d", "hi"):        %!d(string=hi)
Too many arguments: %!(EXTRA type=value)
	Printf("hi", "guys"):      hi%!(EXTRA string=guys)
Too few arguments: %!verb(MISSING)
	Printf("hi%d"):            hi%!d(MISSING)
Non-int for width or precision: %!(BADWIDTH) or %!(BADPREC)
	Printf("%*s", 4.5, "hi"):  %!(BADWIDTH)hi
	Printf("%.*s", 4.5, "hi"): %!(BADPREC)hi
Invalid or invalid use of argument index: %!(BADINDEX)
	Printf("%*[2]d", 7):       %!d(BADINDEX)
	Printf("%.[2]d", 7):       %!d(BADINDEX)

所有的错误都始于“%!”,有时紧跟着单个字符(占位符),并以小括号括住的描述结尾。

也做个最常见的示例,错误格式的占位符。

代码语言:javascript
复制
log.Printf("%t", 1)

打印如下:

代码语言:javascript
复制
%!t(int=1)

6 小结

在格式化 IO 时,%v,打印变量的具体数值。万能打印,会根据变量的类型做调整。%T,打印变量的类型。

END

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 格式化IO库
  • 格式化打印的说明
  • 2 占位符通用说明
  • 3 不同数据类型的数值打印
  • 4 其他标记
  • 5 格式化错误
  • 6 小结
  • END
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档