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

边栏推荐
- 18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
- "14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
- What are Yunna's fixed asset management systems?
- Key structure of ffmpeg -- AVCodecContext
- JS can really prohibit constant modification this time!
- Priority queue (heap)
- Solve the problem of reading Chinese garbled code in sqlserver connection database
- XML配置文件(DTD详细讲解)
- 第16章 OAuth2AuthorizationRequestRedirectWebFilter源码解析
- USB Interface USB protocol
猜你喜欢

Problems encountered in the database

Ffmpeg learning - core module

Senparc.Weixin.Sample.MP源码剖析

Initialize your vector & initializer with a list_ List introduction

Hardware and interface learning summary
![[designmode] composite mode](/img/9a/25c7628595c6516ac34ba06121e8fa.png)
[designmode] composite mode

Detailed explanation of APP functions of door-to-door appointment service

MySQL functions

Key structure of ffmpeg - avframe

Redis high availability - master-slave replication, sentinel mode, cluster
随机推荐
What are the functions of Yunna fixed assets management system?
Open3D 点云随机添加噪声
2022.7.5-----leetcode.729
上门预约服务类的App功能详解
【二叉搜索树】增删改查功能代码实现
【在线聊天】原来微信小程序也能回复Facebook主页消息!
Mysql - CRUD
FFMPEG关键结构体——AVFormatContext
Problems encountered in the database
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
USB Interface USB protocol
【DesignMode】适配器模式(adapter pattern)
Configuring OSPF load sharing for Huawei devices
行列式学习笔记(一)
FFT learning notes (I think it is detailed)
What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
AtCoder Beginner Contest 254【VP记录】
Upgrade openssl-1.1.1p for openssl-1.0.2k
Asynchronous task Whenall timeout - Async task WhenAll with timeout
Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share