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)

  

输出结果:

      F5 F1

      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  }

{Name:xiaoming Age: ID:}

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

三 重复使用操作数

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

    x := int64(0xdeadbeef)

    fmt.Printf("%d %[1]x %#[1]x %#[1]X\r\n", x)

  

输出结果

 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)

输出结果

   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进行打印等待符号,计算完成后退出。

Golang fmt包使用小技巧的更多相关文章

  1. Golang的防坑小技巧

    Golang的防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 作为一名小白,在之前没有接触到编程的小伙伴,难免会踩到一些坑,比如说刚刚入门的时候你需要安装环境,学习Gol ...

  2. golang fmt包

    fmt fmt包实现了类似C语言printf和scanf的格式化I/O.主要分为向外输出内容和获取输入内容两大部分. 向外输出 标准库fmt提供了以下几种输出相关函数. Print Print系列函数 ...

  3. R包的小技巧

    通常我们都是直接使用library(pkg_name)  的形式加载R包,在同一台机器上面,对于我们而言,这个包所在的路径一定是在.libPaths() 路面的,但是对于其他用户而言,这个路径可能不存 ...

  4. golang格式化输出-fmt包用法详解

    golang格式化输出-fmt包用法详解 注意:我在这里给出golang查询关于包的使用的地址:https://godoc.org    声明: 此片文章并非原创,大多数内容都是来自:https:// ...

  5. [转]Golang 中使用 JSON 的小技巧

    taowen是json-iterator的作者. 序列化和反序列化需要处理JSON和struct的关系,其中会用到一些技巧. 原文 Golang 中使用 JSON 的小技巧是他的经验之谈,介绍了一些s ...

  6. SpringBoot小技巧:Jar包换War包

    SpringBoot小技巧:Jar包换War包 情景 我们都知道springBoot中已经内置了tomcat,是不需要我们额外的配置tomcat服务器的,但是有时这也可能是我们的一个瓶颈,因为如果我们 ...

  7. SpringBoot小技巧:修改java可执行jar包内容

    SpringBoot小技巧:修改java可执行jar包内容 情景描述 在生产环境中,有时候我们发现了个小bug,开发迅速修改代码后,很多时候我们不得不重新发布一个新的可执行jar包上去替换掉.但是这样 ...

  8. 『GoLang』fmt包的使用

    目录 1. fmt 包初识 2. 格式化 verb 应用 2.1 通用 2.2 布尔值 2.3 整数 2.4 浮点数与复数 2.5 字符串和 []byte 2.6 指针 2.7 其他 flag 2.8 ...

  9. IDEA的小技巧:1.Java代码不被识别2.目录下创建的文件夹所显示样式不是文件夹,而是"包"图标样式的问题

    在Idea上面一个正常的代码结构是这个样子的,但是有的时候,比如说当我们直接在一个文件夹中随便的创建的时候就会出现一些问题,比如说想让某个地方为代码目录,某个地方为资源目录的时候,直接的创建目录是不成 ...

随机推荐

  1. Python入门 - 环境搭建

    因为本人用的mac系统,所以这里只演示mac系统下python环境的搭建,至于windows和linux系统有类似安装过程,可以参考官方文档. 第一步: 安装python3.6 # Mac OS X ...

  2. 用Vim 加密文本

    Vim强大就在于 可以干任何想要做的事情,比如加密.Fedora 18上给大家做一个测试.首先安装 vim: sudo yum install vim -y然后检验模块是否有加密: vim --ver ...

  3. listviewMyAdapter

    import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory; ...

  4. 【转】Swig 使用指南

    原文链接:https://www.cnblogs.com/elementstorm/p/3142644.html 如何使用 API swig.init({ allowErrors: false, au ...

  5. Mysql服务器SQL模式 (官方精译)

    MySQL服务器可以在不同的SQL模式下运行,并且可以根据sql_mode系统变量的值对不同的客户端应用不同的模式.DBA可以设置全局SQL模式以匹配站点服务器操作需求,并且每个应用程序可以将其会话S ...

  6. iBeacon硬件厂商和产品

    最近项目比较忙,也没有时间写Blog. 这几天因为项目需要采购为数不小的iBeacon,所以花了点时间对市面上主要的iBeacon简单的调研总结了一下. 分享给需要买相关设备进行试验的小伙伴,数据不权 ...

  7. webpack 图片的打包

    1. 在img文件夹下随便找一个小一点的图片放进去. 2.修改entry.js require('../css/index.css'); var demo1 = require('../js/demo ...

  8. vue2入门之vue-cli

    vue-cli vue在web前端可谓是大放异彩,尤其在国内与angular.react有三足鼎立之势.很多人想入门vue2而又苦于不知从何下手.因为vue2是以组件化开发的,最好要搭配webpack ...

  9. 关于Switch case条件语句中无break的用法

    关于Switch case条件语句的另类用法       今天在拜读一位前辈的程序时,遇到了这样一段程序: /***************************/ switch(operation ...

  10. java 后台代码调用接口

    import com.jiuqu.jollykeys.common.util.JsonUtil;import java.io.UnsupportedEncodingException;import j ...