分享
  1. 首页
  2. 文章

Golang fmt包使用小技巧

majianguo · · 4144 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

Golang fmt包使用小技巧


Go语言fmt包实现了类似于C语言printfscanf的格式化I/O函数。格式谓词用%前导,go语言中称为"verb"verbsC派生而来,但更简单。以下是在开发过程中用过的一些实用小技巧。

一 用十六进制打印数组或切片,每个byte两个字符,每两个字符用空格间隔

该功能在通信协议类的开发中使用频繁,向终端设备发送的控制命令及应答信息需要打印在日志中,或调试时打印收发数据,通常这些是十六进制格式的,并且需要每两个字符之间用空格间隔。

 data := []byte{1, 2, 4, 88, 99, 120, 245, 241}
 fmt.Printf("% X\r\n", data)
 fmt.Printf("% x\r\n", data)

输出结果:


01 02 04 58 63 78 F5 F1
01 02 04 58 63 78 f5 f1


二 打印结构体时输出字段名


typePersionstruct {
 Name string
 Age int
 ID string
}
 p := Persion{"xiaoming", 12, "1302222222"}
 fmt.Printf("%v\r\n", p)
 fmt.Printf("%+v\r\n", p)

输出结果:

{xiaoming 12 1302222222}
{Name:xiaoming Age:12 ID:1302222222}

默认的%v打印只有值,%+v可以增加结构体字段名



三 重复使用操作数

经常会遇到同一个值在格式化打印内容中出现多次的情况,go语言提供了重复使用操作数的机制。

 x := int64(0xdeadbeef)
 fmt.Printf("%d %[1]x %#[1]x %#[1]X\r\n", x)

输出结果

3735928559 deadbeef 0xdeadbeef 0XDEADBEEF

四 打印不同进制的数值时增加前缀

 x = 200
 px := &x
 fmt.Printf("%#d %#[1]o %#[1]b %#[1]x %#[1]X %p %#[2]p\r\n", x,px)

输出结果

200 0310 11001000 0xc8 0XC8 0xc042040228 c042040228

#8进制数值增加0;为十六进制%#x增加0x;为十六进制%#X增加0X;%#p抑制了0x;

#对十进制d和二进制b不起作用。


五 打印复杂结构体


开发过程中经常用到复杂的结构体,结构体中带有层层嵌套结构,并且字段是结构体的指针,对这样的字段printf打印的是指针的值,这不是我们需要的。

例如有如下在开发中用到的结构体,avro描述

{
"type": "record",
"name": "intersection",
"fields" : [
{"name": "inter_id", "type": "int"},
{"name": "name", "type": "string"},
{"name": "shape", "type": "int"},
{"name": "primary_unit", "type": "int"},
{"name": "unit_two", "type": "int"},
{"name": "unit_three", "type": "int"},
{"name": "longitude", "type": "double"},
{"name": "latitude", "type": "double"},
{"name": "entrances", "type": {"type": "array", "name": "", "items": {
"type": "record",
"name": "entrance",
"fields" : [
{"name": "en_id", "type": "int"},
{"name": "name", "type": "string"},
{"name": "degree", "type": "int"},
{"name": "orientation", "type": "int"},
{"name": "side_walks", "type": {"type": "array", "name": "", "items": {
"type": "record",
"name": "side_walk",
"fields" : [
{"name": "id", "type": "int"}
]
}}},
{"name": "motor_lanes", "type": {"type": "array", "name": "", "items": {
"type": "record",
"name": "motor_lane",
"fields" : [
{"name": "id", "type": "int"},
{"name": "lane_flow", "type": "int"},
{"name": "has_waiting_area", "type": "boolean"}
]
}}},
{"name": "non_motor_lanes", "type": {"type": "array", "name": "", "items": {
"type": "record",
"name": "non_motor_lane",
"fields" : [
{"name": "id", "type": "int"},
{"name": "lane_flow", "type": "int"}
]
}}},
{"name": "exit_lanes", "type": {"type": "array", "name": "", "items": {
"type": "record",
"name": "exit_lane",
"fields" : [
{"name": "id", "type": "int"},
{"name": "lane_flow", "type": "int"}
]
}}},
{"name": "exit_non_motor_lanes", "type": {"type": "array", "name": "", "items": "non_motor_lane"}}
]
}}}
]
}


生成的代码如下(部分省略)


typeIntersectionstruct {
 InterID int32`json:"inter_id" xorm:"pk notnull"`
 Name string`json:"name"`
 Shape int32`json:"shape"`
 PrimaryUnit int32`json:"primary_unit"`
 UnitTwo int32`json:"unit_two"`
 UnitThree int32`json:"unit_three"`
 Longitude float64`json:"longitude"`
 Latitude float64`json:"latitude"`
 Entrances []*Entrance `json:"entrances" xorm:"-"`
}
typeEntrancestruct {
 InterID int32`json:"-" xorm:"pk notnull"`
 EnID int32`json:"en_id" xorm:"pk notnull"`
 Name string`json:"name"`
 Degree int32`json:"degree"`
 Orientation int32`json:"orientation"`
 SideWalks []*SideWalk `json:"side_walks" xorm:"-"`
 MotorLanes []*MotorLane `json:"motor_lanes" xorm:"-"`
 NonMotorLanes []*NonMotorLane `json:"non_motor_lanes" xorm:"-"`
 ExitLanes []*ExitLane `json:"exit_lanes" xorm:"-"`
 ExitNonMotorLanes []*NonMotorLaneExit `json:"exit_non_motor_lanes" xorm:"-"`
}

如果进行打印,输出只有一层结构,嵌套的部分只有指针值。


要打印完整的结果,有两种方法:一种是用反射实现自定义的print进行深度打印;另外一种是利用json包。

 bData, _ := json.MarshalIndent(dbConf, "", "\t")
 fmt.Println(string(bData))

六 终端程序打印等待


经常会写些工具类软件,如果耗时较长,增加等待输出会使提高用户使用体验。以下引用《Go语言程序设计》的例子。

package main
import (
 "fmt"
 "time"
)
func main() {
 go spinner(100 * time.Millisecond)
 const n = 45
 fibN := fib(n) //slow
 fmt.Printf("\rFibonacci(%d)=%d\n", n, fibN)
}
func spinner(delay time.Duration) {
 for {
 for _, r := range `-\|/` {
 fmt.Printf("\r%c", r)
 time.Sleep(delay)
 }
 }
}
func fib(x int) int {
 if x < 2 {
 return x
 }
 return fib(x-1) + fib(x-2)
}

斐波那契函数计算较慢,开启goroutine进行打印等待符号,计算完成后退出。




















有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:majianguo

查看原文:Golang fmt包使用小技巧

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
4144 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏