Golang fmt包使用小技巧

Golang fmt包使用小技巧


Go語言fmt包實現了相似於C語言printfscanf的格式化I/O函數。格式謂詞用%前導,go語言中稱爲verbverbsC派生而來,但更簡單。如下是在開發過程當中用過的一些實用小技巧。git

一 用十六進制打印數組或切片,每一個byte兩個字符,每兩個字符用空格間隔

該功能在通訊協議類的開發中使用頻繁,向終端設備發送的控制命令及應答信息須要打印在日誌中,或調試時打印收發數據,一般這些是十六進制格式的,而且須要每兩個字符之間用空格間隔。json

    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)

  

輸出結果:ide

{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)

  

輸出結果spa

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進行打印等待符號,計算完成後退出。

相關文章
相關標籤/搜索