admin管理员组文章数量:1130349
前言
我们经常遇到如何将 map[string]interface{} 转化为 struct, 这个过程会用到反射, 通过反射可以实现,不确定的成员依然适用 map[string]interface{} 表示,确定结构后,再将 map[string]interface{} 解析为具体的某个结构。
主要使用的是 mapstructure 来实现,将 map 转换称 struct
一个第三方库,地址:https://github/mitchellh/mapstructure
json 转换成 map ,然后 map 转换成 struct
json 转换成 struct
json 转换成 struct 只需要使用 json.unmashal 即可
type User struct {
Name string
FansCount int64
}
func TestJsonUnmarshal(t *testing.T) {
const jsonStream = `
{"name":"ethancai", "fansCount": 9223372036854775807}
`
var user User /前言
我们经常遇到如何将 map[string]interface{} 转化为 struct, 这个过程会用到反射, 通过反射可以实现,不确定的成员依然适用 map[string]interface{} 表示,确定结构后,再将 map[string]interface{} 解析为具体的某个结构。
主要使用的是 mapstructure 来实现,将 map 转换称 struct
一个第三方库,地址:https://github/mitchellh/mapstructure
json 转换成 map ,然后 map 转换成 struct
json 转换成 struct
json 转换成 struct 只需要使用 json.unmashal 即可
type User struct {
Name string
FansCount int64
}
func TestJsonUnmarshal(t *testing.T) {
const jsonStream = `
{"name":"ethancai", "fansCount": 9223372036854775807}
`
var user User /本文标签: 语言Mapstructure
版权声明:本文标题:Go 语言 mapstructure 使用 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1755047275a2759793.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论