当前位置:网站首页>Go learning --- structure to map[string]interface{}
Go learning --- structure to map[string]interface{}
2022-07-06 00:15:00 【Duck boss】
One 、 The structure turns map[string]interface{} The type of question
package main
import (
"encoding/json"
"fmt"
)
// Define a structure
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
user := User{
Name: " Xiao Wang ",
Age: 25,
}
m, _ := json.Marshal(&user)
var ma map[string]interface{}
json.Unmarshal(m,&ma)
for s, i := range ma {
fmt.Printf("s=%v i=%v i Of type:%T\n",s,i,i)
}
}
resolvent :
package main
import (
"fmt"
"reflect"
)
// Define a structure
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
func ToMap(in interface{},tagName string)(map[string]interface{},error) {
myMap := make(map[string]interface{})
v := reflect.ValueOf(in)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return nil,fmt.Errorf(" Type error ",v)
}
t := v.Type()
// Traverse structure fields
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
get := field.Tag.Get(tagName)
if get != "" {
myMap[get] = v.Field(i).Interface()
}
}
return myMap,nil
}
func main() {
user := User{
Name: " Xiao Wang ",
Age: 25,
}
toMap, _ := ToMap(&user, "json")
for s, i := range toMap {
fmt.Printf("s=%v i=%v i Of type:%T\n",s,i,i)
}
}
边栏推荐
- 数据库遇到的问题
- Wechat applet -- wxml template syntax (with notes)
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- The difference of time zone and the time library of go language
- PHP determines whether an array contains the value of another array
- 云呐|固定资产管理系统功能包括哪些?
- Add noise randomly to open3d point cloud
- LeetCode 6005. The minimum operand to make an array an alternating array
- Global and Chinese market of digital serial inverter 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
上门预约服务类的App功能详解
Hardware and interface learning summary
Detailed explanation of APP functions of door-to-door appointment service
[designmode] composite mode
Open source CRM customer relationship system management system source code, free sharing
[designmode] Decorator Pattern
N1 # if you work on a metauniverse product [metauniverse · interdisciplinary] Season 2 S2
GD32F4xx uIP协议栈移植记录
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
How much do you know about the bank deposit business that software test engineers must know?
随机推荐
FFMPEG关键结构体——AVFormatContext
Mathematical model Lotka Volterra
MySQL之函数
跟着CTF-wiki学pwn——ret2libc1
Browser local storage
Senparc.Weixin.Sample.MP源码剖析
[Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
云呐|固定资产管理系统主要操作流程有哪些
OS i/o devices and device controllers
C # input how many cards are there in each of the four colors.
Problems encountered in the database
【luogu P3295】萌萌哒(并查集)(倍增)
7.5模拟赛总结
数据库遇到的问题
从底层结构开始学习FPGA----FIFO IP核及其关键参数介绍
Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
wx.getLocation(Object object)申请方法,最新版
用列錶初始化你的vector&&initializer_list簡介
Redis high availability - master-slave replication, sentinel mode, cluster
【QT】Qt使用QJson生成json文件并保存