zoukankan      html  css  js  c++  java
  • Golang fmt包使用小技巧

    Golang fmt包使用小技巧


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

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

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

        data := []byte{1, 2, 4, 88, 99, 120, 245, 241}
    
        fmt.Printf("% X
    ", data)
    
        fmt.Printf("% x
    ", 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
    ", p)
    
        fmt.Printf("%+v
    ", 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
    ", x)
    

      

    输出结果

    3735928559 deadbeef 0xdeadbeef 0XDEADBEEF

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

        x = 200
    
        px := &x
    
        fmt.Printf("%#d %#[1]o %#[1]b %#[1]x %#[1]X %p %#[2]p
    ", 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, "", "	")
    
        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("
    Fibonacci(%d)=%d
    ", n, fibN)
    
    }
    
    
    func spinner(delay time.Duration) {
    
        for {
    
            for _, r := range `-|/` {
    
                fmt.Printf("
    %c", r)
    
                time.Sleep(delay)
    
            }
    
        }
    
    }
    
    
    func fib(x int) int {
    
        if x < 2 {
    
            return x
    
        }
    
    
        return fib(x-1) + fib(x-2)
    
    }
    

      

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



















  • 相关阅读:
    javascript控制页面(含iframe进行页面跳转)跳转、刷新的方法汇总
    window下安装docker
    http协议
    php环境选择
    jsmooth和exe4j
    域名解析
    clientHeight,offsetHeight,scrollHeight迷一样的三个值
    LinkedHashMap和hashMap和TreeMap的区别
    fiddler抓包
    mac下配置openfire
  • 原文地址:https://www.cnblogs.com/majianguo/p/7894557.html
Copyright © 2011-2022 走看看