当前位置:网站首页>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)
}
}
边栏推荐
- LeetCode 6006. Take out the least number of magic beans
- 上门预约服务类的App功能详解
- wx. Getlocation (object object) application method, latest version
- Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste
- Atcoder beginer contest 254 [VP record]
- [designmode] composite mode
- DEJA_VU3D - Cesium功能集 之 055-国内外各厂商地图服务地址汇总说明
- [QT] QT uses qjson to generate JSON files and save them
- MySQL functions
- 数据库遇到的问题
猜你喜欢
Qt QPushButton详解
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
XML configuration file (DTD detailed explanation)
云呐|固定资产管理系统功能包括哪些?
Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
openssl-1.0.2k版本升级openssl-1.1.1p
Yunna | what are the main operating processes of the fixed assets management system
Open source CRM customer relationship system management system source code, free sharing
Learn PWN from CTF wiki - ret2libc1
Knowledge about the memory size occupied by the structure
随机推荐
Chapter 16 oauth2authorizationrequestredirectwebfilter source code analysis
Codeforces Round #804 (Div. 2)【比赛记录】
Global and Chinese markets of POM plastic gears 2022-2028: Research Report on technology, participants, trends, market size and share
AtCoder Beginner Contest 258【比赛记录】
[designmode] Decorator Pattern
wx. Getlocation (object object) application method, latest version
Atcoder beginer contest 258 [competition record]
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
wx.getLocation(Object object)申请方法,最新版
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
QT -- thread
Open source CRM customer relationship system management system source code, free sharing
云呐|固定资产管理系统功能包括哪些?
SQLServer连接数据库读取中文乱码问题解决
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
MySQL functions
Yunna | what are the main operating processes of the fixed assets management system
Effet Doppler (déplacement de fréquence Doppler)
Learn PWN from CTF wiki - ret2libc1
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)