Golang fmt包使用小技巧
Go語言fmt包實現了類似於C語言printf和scanf的格式化I/O函數。格式謂詞用%前導,go語言中稱為”verb”。verbs從C派生而來,但更簡單。以下是在開發過程中用過的一些實用小技巧。
一 用十六進制打印數組或切片,每個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進行打印等待符號,計算完成后退出。
