golang 中把struct 转成json格式输出

 package main

 import (
"encoding/json"
"fmt"
) type Person struct {
Name string `json:"name,omitempty"`
DoB string `json:"dob,omitempty"`
Age string `json:"-,omitempty"`
} type Singer struct {
Person
MusicGenre string `json:"music_genre,omitempty"`
Age string `json:"ageee,omitempty"`
} func ToJSON(s interface{}) (string, error) {
bs, err := json.Marshal(s)
if err != nil {
return "", err
}
// fmt.Println(bs)
return string(bs), nil
} func main() {
s := Singer{
Person{"John Singer",
"01-02-1975",
"13"},
"pop", "12" }
sJSON, _ := ToJSON(s)
fmt.Println(sJSON)
// {"name":"John Singer","dob":"01-02-1975","music_genre":"pop"}
}

  

---------------------------------

One of the things that I missed the most, apart from generics, when coming to Go while having Java background was the lack of inheritance. In JVM it was pretty simple, as you can define a parent and child classes, then have a common behavior present all the children objects. This is not the case in Go, and I had to learn how to work around this. Here, we don't have inheritance, we have the composition, and for that, we have a pretty useful technique called embedding structs.

Manual composition vs embedding

In the classical approach to composition, we add what we'd call a parent class as another field in the child one. In Go terms, we should first define a parent struct with common attributes:

type Person struct {
Name string
}

Then we should add an attribute of type Person to another struct which we want to use that common features from the parent:

type Singer struct {
Parent Person
MusicGenre string
}

This may not seem like a huge improvement, but we can use a shorter notation and embedPerson into Singer by skipping the attribute name and just leave its type:

type Singer struct {
Person
MusicGenre string
}

In both cases, when creating an instance of Singer inline we need to specify a definition of parent struct with its name - when embedding it is the name of that struct:

// manual composition
s0 := embedding.Singer{
Parent: embedding.Person{Name: "John Singer"},
MusicGenre: "pop",
} // embedding
s1 := embedding.Singer{
Person: embedding.Person{Name: "John Singer"},
MusicGenre: "pop",
}

The difference is larger than just saving a few characters in the struct definition. First of all, when doing it manually we'd have to use attribute name (Parent) all the time:

// manual composition
fmt.Println(s0.Parent.Name)

While with embedding we can (but don't have to) refer to the attributes of the embedded struct as if they were defined in the child:

// embedding
fmt.Println(s1.Parent.Name)
fmt.Println(s1.Name)

Another interesting thing is that if we want to build a Singer struct by adding attributes to an empty instance, again we get a feeling of the inheritance:

// manual composition
s0 := embedding.Singer{MusicGenre: "pop"}
s0.Parent.Name = "John Singer" // we have to do this explicitly
fmt.Println(s0.Parent.Name) // embedding
s1 := embedding.Singer{MusicGenre: "pop"}
// we can be explicit with
// s1.Person.Name = "John Doe"
// but we can as well do this:
s1.Name = "John Singer"
fmt.Println(s1.Parent.Name)

Calling inherited functions

Another useful feature of embedding structs is that we can call parent's functions that were inherited as if they were defined on a child struct. For example, we can add a Talk(..)function on Person and use it on an instance of Singer:

type Person struct {
...
} func (p Person) Talk(message string) {
fmt.Printf("%s (a person) says \"%s\"\n", p.Name, message)
} func main() {
s := Singer{}
s.Name = "John Doe"
s.Talk("Hello, reader!")
// John Doe (a person) says "Hello, reader!"
}

Awesome! Remember that the attributes and function are promoted to the child struct only if they are not overwritten there. If we define a Talk(..) function directly on Singer, the one from Person would never be called:

...
func (p Singer) Talk(message string) {
fmt.Printf("Singer %s says \"%s\"\n", p.Name, message)
}
func main() {
s := Singer{}
s.Name = "John Doe"
s.Talk("Hello, again!")
// Singer John Singer says "Hello again!"
}

The trick is when a function that is promoted calls another one. For example, if we define a Type() function that would return the struct identifier on both Person and Singer, then call it within Talk function that would be promoted from the parent, we would get the Type() from the parent as well. The reason for this is that at the time of executing the function, Go does not realize we are dealing with some inheritance stuff and we don't go back to the original caller to see what the context is:

func (p Person) Type() string {
return "PERSON"
}
func (p Person) Talk(message string) {
fmt.Printf("%s (type=%s) says \"%s\"\n", p.Name, p.Type(), message)
}
...
func (s Singer) Type() string {
return "SINGER"
} func (s Singer) Sing(title string) {
fmt.Printf("%s (type=%s) sings %s in the style of %s.\n", s.Name, s.Type(), title, s.MusicGenre)
}
...
func main() {
s := Singer{MusicGenre: "rock"}
s.Name = "Johny Singerra"
s.Sing("Welcome to the forest")
// Johny Singerra (type=SINGER) sings Welcome to the forest in the style of rock.
s.Talk("Hello!")
// Johny Singerra (type=PERSON) says "Hello!"
}

Hiding JSON properties

Another interesting fact is the way Go handler JSON tags that can identify attributes of a struct. For example, we can marshal Singer to JSON and see that both its own and the inherited properties end up in the document:

type Person struct {
Name string `json:"name,omitempty"`
DoB string `json:"dob,omitempty"`
}
...
type Singer struct {
Person
MusicGenre string `json:"music_genre,omitempty"`
}
...
func (s Singer) ToJSON() (string, error) {
bs, err := json.Marshal(s)
if err != nil {
return "", err
}
return string(bs), nil
}
...
func main() {
s := embedding.Singer{
Person: embedding.Person{
Name: "John Singer",
DoB: "01-02-1975",
},
MusicGenre: "pop",
}
sJSON, _ := s.ToJSON()
fmt.Println(sJSON)
// {"name":"John Singer","dob":"01-02-1975","music_genre":"pop"}
}

It's great that both name and dob properties are there, but what if we want to hide some of those for JSON marshaler? Let's create a MusicStar struct where we'll add a nickname to the Singer, but at the same time we'd like to hide the date of birth (since we want to keep it as a secret):

type MusicStar struct {
Singer
Nickname string `json:"nickname,omitempty"`
DoB string `json:"-,omitempty"`
} func (ms MusicStar) ToJSON() (string, error) {
bs, err := json.Marshal(ms)
if err != nil {
return "", err
}
return string(bs), nil
}

Note that we've added a DoB field but added a - as JSON tag to indicate that this field should be removed from marshaling. Unfortunately, that doesn't work:

func main() {
ms := embedding.MusicStar{
Nickname: "Starry",
Singer: embedding.Singer{
Person: embedding.Person{
Name: "Joe Star",
DoB: "01-02-1975",
},
MusicGenre: "pop",
},
}
msJSON, _ := ms.ToJSON()
fmt.Println(msJSON)
// "name":"Joe Star","dob":"01-02-1975","music_genre":"pop","nickname":"Starry"}
}

That is because although Go sees our - JSON tag, it recognizes it as undefined and go deeper into embedded structs to see if there is a field that matches the name, but has some concrete tag defined. It finds one, so that is being used. We can, however, trick the language into hiding that nested DoB, by defining the same property with the same JSON tag in the top-level struct. This way the DoB from Person will never be promoted to the top level JSON object, since its top-level value is empty, therefore the empty string overwrites anything that comes from Person:

type MusicStar struct {
Singer
Nickname string `json:"nickname,omitempty"`
DoB string `json:"dob,omitempty"`
}
...
msJSON, _ := ms.ToJSON()
fmt.Println(msJSON)
// {"name":"Joe Star","music_genre":"pop","nickname":"Starry"}

As you can see, embedded structs solve some of the things we would achieve via classical inheritance, but it's necessary to understand how it works to avoid unexpected behaviors and gotchas. The full source code of these examples is available on Github.

golang embedded structs的更多相关文章

  1. 从OOP的角度看Golang

    资料来源 https://github.com/luciotato/golang-notes/blob/master/OOP.md?hmsr=toutiao.io&utm_medium=tou ...

  2. golang Methods on structs

    原文:http://golangtutorials.blogspot.com/2011/06/methods-on-structs.html snmp 下载,有空学习一下! https://sourc ...

  3. Go语言(golang)开源项目大全

    转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析 ...

  4. [转]Go语言(golang)开源项目大全

    内容目录 Astronomy 构建工具 缓存 云计算 命令行选项解析器 命令行工具 压缩 配置文件解析器 控制台用户界面 加密 数据处理 数据结构 数据库和存储 开发工具 分布式/网格计算 文档 编辑 ...

  5. Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目

    Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...

  6. golang sync.noCopy 类型 —— 初探 copylocks 与 empty struct

    问题引入 学习golang(v1.16)的 WaitGroup 代码时,看到了一处奇怪的用法,见下方类型定义: type WaitGroup struct { noCopy noCopy ... } ...

  7. Golang通脉之面向对象

    面向对象的三大特征: 封装:隐藏对象的属性和实现细节,仅对外提供公共访问方式 继承:使得子类具有父类的属性和方法或者重新定义.追加属性和方法等 多态:不同对象中同种行为的不同实现方式 Go并不是一个纯 ...

  8. 【转载】关于Embedded Linux启动的经典问题

    转载自:http://linux.chinaunix.net/techdoc/install/2009/04/13/1107608.shtml 发信人: armlinux (armlinux), 信区 ...

  9. [转]50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs

    http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/ 50 Shades of Go: Traps, Gotc ...

随机推荐

  1. Linux上,最常用的一批命令解析(10年精选)

    Linux这么多命令,通常会让初学者望而生畏.下面是我结合日常工作,以及在公司的内部培训中,针对对Linux不是很熟悉的同学,精选的一批必须要搞懂的命令集合.任何一个命令其实都是可以深入的,比如tai ...

  2. U盘自动复制文件

    1.建立一个文本文档,WIN+R 里面打NOTEPAD ,或者自己新建一个都一样. 2.把下面的代码复制进去 set fso=createobject("scripting.filesyst ...

  3. kali PIN码破解

    airmon-ng start wlan0   //开启网卡airodump-ng wlan0mon    //监听模式,查找开启wps的apreaver -i wlan0mon -b [ap’s m ...

  4. jquery设置input不可编辑,背景变灰,鼠标变禁止

    先看效果 $("#id").attr("onfocus", "this.blur()"); $("#id").css(& ...

  5. 洛谷 题解 P1196 【[NOI2002]银河英雄传说】

    并查集大难题. 看了题解之后才有思路,调了很久很久才AC,当然要写一篇题解来纪念一下. 先来分析一下这些指令的特点,很容易发现对于每个M指令,只可能一次移动整个队列,并且是把两个队列首尾相接合并成一个 ...

  6. 初识 docker

    一.安装Docker 我使用的是腾讯云上的centos 7. docker -v 查看是否已经安装有docker 如果有 systemctl stop docker 停止docker服务 查看当前版本 ...

  7. N分成不同的数相乘使答案最大

    题意:http://acm.hdu.edu.cn/showproblem.php?pid=5976 首先队友想出了分的越多答案越多. 我们就:2,3,4,5,6...多出来的尽量往小了加就行了. #d ...

  8. 树链剖分 树剖求lca 学习笔记

    树链剖分 顾名思义,就是把一课时分成若干条链,使得它可以用数据结构(例如线段树)来维护 一些定义: 重儿子:子树最大的儿子 轻儿子:除了重儿子以外的儿子 重边:父节点与重儿子组成的边 轻边:除重边以外 ...

  9. Django新手入门必看

    pip install django==2.1.7 (现在Django3.0出来,推荐大家可以使用一下Django3.0) pip list查看

  10. XPath库详解

    目录 xpath入门 获取节点 获取所有节点 获取子节点 获取父节点 属性匹配 根据属性值匹配节点 属性多值匹配 多属性匹配 文本获取 按序选择 节点轴选择 补充 xpath的运算符介绍 xpath轴 ...