MixGo XFMT 解决 Golang 结构体嵌套格式化打印指针地址

栏目: 软件资讯 · 发布时间: 4年前

内容简介:OpenMix 出品:https://openmix.org Mix XFMT 可以打印结构体嵌套指针地址内部数据的格式化库 Formatting library that can print the internal data of the nested pointer address of the struct Github https...
OpenMix 出品:https://openmix.org

Mix XFMT

可以打印结构体嵌套指针地址内部数据的格式化库

Formatting library that can print the internal data of the nested pointer address of the struct

Github

https://github.com/mix-go/xfmt

Overview

在 Golang 中使用 fmt 打印结构体时,无法打印指针字段内部的数据结构,导致增加 debug 难度,该库可以解决这个问题。

Installation

  • 安装
go get -u github.com/mix-go/xfmt

Usage

  • 支持的方法,与 fmt 系统库完全一致

    • Sprintf(format string, args ...interface{}) string
    • Sprint(args ...interface{}) string
    • Sprintln(args ...interface{}) string
    • Printf(format string, args ...interface{})
    • Print(args ...interface{})
    • Println(args ...interface{})
  • 支持 Tag 忽略某个引用字段
type Foo struct {
    Bar *Bar `xfmt:"-"`
}
  • 使用

包含指针的结构体

type Level3 struct {
    Name string
}

type Level2 struct {
    Level3 *Level3 `xfmt:"-"`
    Name   string
}

type Level1 struct {
    Name     string
    Level2   *Level2
    Level2_1 *Level2
}

创建变量

l3 := Level3{Name: "Level3"}
l2 := Level2{Name: "Level2", Level3: &l3}
l1 := Level1{Name: "Level1", Level2: &l2, Level2_1: &l2}

打印对比

  • fmt 打印
fmt.Println(fmt.Sprintf("%+v", l1))
{Name:Level1 Level2:0xc00009c500 Level2_1:0xc00009c500}
  • xfmt 打印:其中 Level3 被定义的 tag 忽略,Level2_1 由于和 Level2 是同一个指针因此后面的忽略处理
fmt.Println(xfmt.Sprintf("%+v", l1))
{Name:Level1 Level2:0xc00009c500:&{Level3:0xc00007f030 Name:Level2} Level2_1:0xc00009c500}

License

Apache License Version 2.0, http://www.apache.org/licenses/


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

用户故事地图

用户故事地图

Jeff Patton / 李涛、向振东 / 清华大学出版社 / 2016-4-1 / 59.00元

用户故事地图作为一种有效的需求工具,越来越广泛地应用于开发实践中。本书以用户故事地图为主题,强调以合作沟通的方式来全面理解用户需求,涉及的主题包括怎么以故事地图的方式来讲用户需求,如何分解和优化需求,如果通过团队协同工作的方式来积极吸取经验教训,从中洞察用户的需求,开发真正有价值的、小而美的产品和服务。本书适合产品经理、用户体验设计师、产品负责人、业务分析师、IT项目经理、敏捷教练和精益教练阅读和......一起来看看 《用户故事地图》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具