当前位置:网站首页>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)
}
}
边栏推荐
- Upgrade openssl-1.1.1p for openssl-1.0.2k
- Choose to pay tribute to the spirit behind continuous struggle -- Dialogue will values [Issue 4]
- LeetCode 6004. Get operands of 0
- Shardingsphere source code analysis
- Teach you to run uni app with simulator on hbuilderx, conscience teaching!!!
- QT QPushButton details
- Zhuan: in the future, such an organization can withstand the risks
- 关于结构体所占内存大小知识
- 关于slmgr命令的那些事
- DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
猜你喜欢
Mysql - CRUD
关于slmgr命令的那些事
China Jinmao online electronic signature, accelerating the digitization of real estate business
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
Gd32f4xx UIP protocol stack migration record
PV static creation and dynamic creation
LeetCode 1189. Maximum number of "balloons"
Learn PWN from CTF wiki - ret2libc1
MySql——CRUD
认识提取与显示梅尔谱图的小实验(观察不同y_axis和x_axis的区别)
随机推荐
NSSA area where OSPF is configured for Huawei equipment
QT--线程
OS i/o devices and device controllers
SQLServer连接数据库读取中文乱码问题解决
GD32F4xx uIP协议栈移植记录
What are the functions of Yunna fixed assets management system?
JS can really prohibit constant modification this time!
[designmode] adapter pattern
Problems encountered in the database
亲测可用fiddler手机抓包配置代理后没有网络
[Luogu cf487e] tours (square tree) (tree chain dissection) (line segment tree)
shardingsphere源码解析
DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
mysql-全局锁和表锁
Tools to improve work efficiency: the idea of SQL batch generation tools
Browser local storage
QT a simple word document editor
【GYM 102832H】【模板】Combination Lock(二分图博弈)
How much do you know about the bank deposit business that software test engineers must know?
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!