当前位置:网站首页>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)
}
}
边栏推荐
- Transport layer protocol ----- UDP protocol
- QT QPushButton details
- 选择致敬持续奋斗背后的精神——对话威尔价值观【第四期】
- 教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
- [designmode] Decorator Pattern
- MySql——CRUD
- MySql——CRUD
- 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?
- 7.5 decorator
- XML configuration file (DTD detailed explanation)
猜你喜欢
After summarizing more than 800 kubectl aliases, I'm no longer afraid that I can't remember commands!
Key structure of ffmpeg - avformatcontext
Hardware and interface learning summary
时区的区别及go语言的time库
硬件及接口学习总结
Ffmpeg learning - core module
About the slmgr command
Doppler effect (Doppler shift)
Single merchant v4.4 has the same original intention and strength!
亲测可用fiddler手机抓包配置代理后没有网络
随机推荐
Codeforces Round #804 (Div. 2)【比赛记录】
如何解决ecology9.0执行导入流程流程产生的问题
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
Yunna | what are the main operating processes of the fixed assets management system
[designmode] composite mode
云呐|固定资产管理系统主要操作流程有哪些
Learn PWN from CTF wiki - ret2libc1
USB Interface USB protocol
Knowledge about the memory size occupied by the structure
什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
"14th five year plan": emphasis on the promotion of electronic contracts, electronic signatures and other applications
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
第16章 OAuth2AuthorizationRequestRedirectWebFilter源码解析
Senparc. Weixin. Sample. MP source code analysis
Browser local storage
【NOI模拟赛】Anaid 的树(莫比乌斯反演,指数型生成函数,埃氏筛,虚树)
权限问题:source .bash_profile permission denied
PHP determines whether an array contains the value of another array
Key structure of ffmpeg - avframe
Initialize your vector & initializer with a list_ List introduction