当前位置:网站首页>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)
}
}

边栏推荐
- Knowledge about the memory size occupied by the structure
- wx. Getlocation (object object) application method, latest version
- MySQL global lock and table lock
- AtCoder Beginner Contest 254【VP记录】
- Yunna | what are the main operating processes of the fixed assets management system
- C # input how many cards are there in each of the four colors.
- LeetCode 1189. Maximum number of "balloons"
- 妙才周刊 - 8
- 【GYM 102832H】【模板】Combination Lock(二分图博弈)
- JS 这次真的可以禁止常量修改了!
猜你喜欢

FFMPEG关键结构体——AVFrame

Hardware and interface learning summary

Mathematical model Lotka Volterra
![[designmode] Decorator Pattern](/img/65/457e0287383d0ca9a28703a63b4e1a.png)
[designmode] Decorator Pattern

跟着CTF-wiki学pwn——ret2libc1

【在线聊天】原来微信小程序也能回复Facebook主页消息!

Senparc.Weixin.Sample.MP源码剖析

Qt QPushButton详解

Tools to improve work efficiency: the idea of SQL batch generation tools

如何解决ecology9.0执行导入流程流程产生的问题
随机推荐
跟着CTF-wiki学pwn——ret2libc1
Key structure of ffmpeg -- AVCodecContext
[designmode] composite mode
NSSA area where OSPF is configured for Huawei equipment
How much do you know about the bank deposit business that software test engineers must know?
What is a humble but profitable sideline?
Browser local storage
QT QPushButton details
The use of El cascader and the solution of error reporting
Redis high availability - master-slave replication, sentinel mode, cluster
mysql-全局锁和表锁
Codeforces round 804 (Div. 2) [competition record]
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
提升工作效率工具:SQL批量生成工具思想
Senparc.Weixin.Sample.MP源码剖析
Problem solving win10 quickly open ipynb file
Ffmpeg learning - core module
XML configuration file (DTD detailed explanation)
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
数据库遇到的问题