golang 中 map 转 struct
golang 中 map 转 struct
package main
import (
"fmt"
"github.com/goinggo/mapstructure"
)
type Person struct {
Name string
Age int
}
func MapToStruct() {
mapInstance := make(map[string]interface{})
mapInstance["Name"] = "liang637210"
mapInstance["Age"] = 28
var person Person
//将 map 转换为指定的结构体
if err := mapstructure.Decode(mapInstance, &person); err != nil {
fmt.Println(err)
}
fmt.Printf("map2struct后得到的 struct 内容为:%v", person)
}
func main(){
MapToStruct()
}
golang 中 json转 map
package main
import (
"fmt"
"encoding/json"
)
func JsonToMap() {
jsonStr := `
{
"name":"liangyongxing",
"age":12
}
`
var mapResult map[string]interface{}
//使用 json.Unmarshal(data []byte, v interface{})进行转换,返回 error 信息
if err := json.Unmarshal([]byte(jsonStr), &mapResult); err != nil {
fmt.Println(err)
}
fmt.Println(mapResult)
}
func main(){
JsonToMap()
}
golang 中 map 转 struct的更多相关文章
- Golang中map的三种声明方式和简单实现增删改查
package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...
- golang中map原理剖析
1. golang中的map有自己的一套实现原理,其核心是由hmap和bmap两个结构体实现的 2. 初始化map package main func main() { // 初始化一个可容纳10个 ...
- golang中map并发读写问题及解决方法
一.map并发读写问题 如果map由多协程同时读和写就会出现 fatal error:concurrent map read and map write的错误 如下代码很容易就出现map并发读写问题 ...
- Go_14:GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- GoLang中 json、map、struct 之间的相互转化
1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...
- golang struct 转map 及 map[string]*Struct 初始化和遍历
package main import ( "encoding/json" "errors" "fmt" "reflect&quo ...
- 说说不知道的Golang中参数传递
本文由云+社区发表 导言 几乎每一个C++开发人员,都被面试过有关于函数参数是值传递还是引用传递的问题,其实不止于C++,任何一个语言中,我们都需要关心函数在参数传递时的行为.在golang中存在着m ...
- golang之map的使用声明
1.map的基本介绍 map是key-value数据结构,又称为字段或者关联数组.类似其它编程语言的集合,在编程中是经常使用到的 2.map的声明 1)基本语法 var map 变量名 map[key ...
- [转] golang中struct、json、map互相转化
一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age ...
随机推荐
- linux系列(十九):firewall-cmd命令
1.命令格式 firewall-cmd [选项] [参数] 2.命令功能: 简单来说是一个防火墙管理工具. 3.简单使用: systemctl start firewalld # 启动, system ...
- Spring - 环境安装
安装IDEA的非Community版本和Java的包之后就可以用Java来HelloWorld了. 然后去这个链接:https://github.com/spring-guides/gs-rest-s ...
- Windows下OpenFOAM开发及使用环境配置指南 (2)【转载】
转载自:http://openfoam.blog.sohu.com/158751915.html *************************************************** ...
- [转载]workbench分网---mapped face meshing面映射网格划分
原文地址:face meshing面映射网格划分">workbench分网---mapped face meshing面映射网格划分作者:一丝尘埃 face meshing面映射网格划 ...
- BZOJ3781小B的询问
莫队裸题. 维护的时候有的打法是利用(a-1)^2==a^2-2*a+1转移,也可以,但是通用性不太够. 下面的打法就是先把这个点的贡献删掉,然后更新这个点,再把这个点的贡献加回来,这种解法更加通用一 ...
- C#Winform ListView中没有Item双击事件的两种实现方法!
第一种: //if (this.listView1.FocusedItem != null) //{ // if (this.listView1.SelectedItems != null) // { ...
- Kafka、RabbitMQ、RocketMQ、ActiveMQ消息中间件的对比--多年生产经验实践总结
引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,前段时间我们自家的产品 RocketMQ (MetaQ的内核) 也顺利开源,得到大家的关注. ...
- OpenJudge计算概论-取石子游戏
OpenJudge计算概论-取石子游戏[函数递归练习] /*====================================================================== ...
- 使用requirejs+vue 打造 无需编译发布便捷修改调整的模块开发方案 (一)
前言 不知道大家有没有这种感觉,现在流行的很多前端技术,基本上都基于webpack编译,当然不是说这种方案不好,在标准的开发流程运行中,这种方式其实也挺不错,管理方便,代码统一. 痛点:项目不是单独针 ...
- AppCompatTextView可改变文本字体大小
有这样一个需求,要求在一个列表中的每个条目中展示字数不限个数的文本.而且每个条目的宽度都是固定的,展示的文本如果过长,不可以用省略号显示,只能动态的调整(缩小)文本的字号来达到文本能完全显示的效果,而 ...