Golang fmt包使用小技巧

Golang fmt包使用小技巧


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

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

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

1
2
3
4
5
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

 


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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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语言提供了重复使用操作数的机制。

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

  

输出结果

3735928559 deadbeef 0xdeadbeef 0XDEADBEEF

 

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

1
2
3
4
5
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"}}

]

}}}

]

}
复制代码

 


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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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包。

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

  

六 终端程序打印等待


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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进行打印等待符号,计算完成后退出。



















posted @   majianguo  阅读(1760)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示