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的更多相关文章

  1. Golang中map的三种声明方式和简单实现增删改查

    package main import ( "fmt" ) func main() { test3 := map[string]string{ "one": & ...

  2. golang中map原理剖析

    1. golang中的map有自己的一套实现原理,其核心是由hmap和bmap两个结构体实现的 2.  初始化map package main func main() { // 初始化一个可容纳10个 ...

  3. golang中map并发读写问题及解决方法

    一.map并发读写问题 如果map由多协程同时读和写就会出现 fatal error:concurrent map read and map write的错误 如下代码很容易就出现map并发读写问题 ...

  4. Go_14:GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  5. GoLang中 json、map、struct 之间的相互转化

    1. golang 中 json 转 struct <1. 使用 json.Unmarshal 时,结构体的每一项必须是导出项(import field).也就是说结构体的 key 对应的首字母 ...

  6. golang struct 转map 及 map[string]*Struct 初始化和遍历

    package main import ( "encoding/json" "errors" "fmt" "reflect&quo ...

  7. 说说不知道的Golang中参数传递

    本文由云+社区发表 导言 几乎每一个C++开发人员,都被面试过有关于函数参数是值传递还是引用传递的问题,其实不止于C++,任何一个语言中,我们都需要关心函数在参数传递时的行为.在golang中存在着m ...

  8. golang之map的使用声明

    1.map的基本介绍 map是key-value数据结构,又称为字段或者关联数组.类似其它编程语言的集合,在编程中是经常使用到的 2.map的声明 1)基本语法 var map 变量名 map[key ...

  9. [转] golang中struct、json、map互相转化

    一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age ...

随机推荐

  1. Greenplum 函数 gp_dist_random

    转载自:https://yq.aliyun.com/articles/7593 函数作用: gp_dist_random('gp_id')本质上就是在所有节点查询gp_id, gp_dist_rand ...

  2. 关于request.getServletPath(),request.getContextPath()的总结

    1. getServletPath():获取能够与“url-pattern”中匹配的路径,注意是完全匹配的部分,*的部分不包括. 2.getContextPath():获取项目的根路径

  3. 2-set 1823: [JSOI2010]满汉全席

    这个题告诉我变量循环使用,一定要赋好初值!!!!!! 一定要赋好初值!!!!!!一定要赋好初值!!!!!!一定要赋好初值!!!!!! #include<iostream>#include& ...

  4. [转]CentOS 7安装Python3.6过程(让linux系统共存Python2和Python3环境)

    CentOS 7系统自带了python2,不过可以不用2版本,直接使用python3运行python脚本就可以,但是千万别去动系统自带的python2,因为有程序依赖目前的python2环境,比如yu ...

  5. Tkinter 之Canvas画布

    一.参数说明 参数 作用 background(bg) 指定 Canvas 的背景颜色 borderwidth(bd) 指定 Canvas 的边框宽度 closeenough 指定一个距离,当鼠标与画 ...

  6. 20165223《网络对抗技术》Exp 9 Web安全基础

    目录 -- Web安全基础 ★ 实验说明 实验目标 基础问答 实验准备 ★ 实验内容 SQL注入攻击 1. 命令注入(Command Injection) 2. 数字型注入(Numeric SQL I ...

  7. bash常用的快捷键

    bash常用快捷键 快捷键 作用 CTRL+A 把光标移动到命令行开头.如果我们输入的命令过长,则在想要把光标移动到命令行开头时使用 CTRL+E 光标移动到命令行行尾 CTRL+C 强制中止当前命令 ...

  8. Qtcreator远程调试出现“The selected build of GDB does not support Python scripting.It cannot be used .."

      版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/aristolto/article/details/77370853 之前使用的是Qt4.7后来换 ...

  9. Python问题:error: Microsoft Visual C++ 9.0 is required

    Python问题:error: Microsoft Visual C++ 9.0 is required 原因是缺少编译C的 VCForPython包. 解决办法: 安装VCForPython即可. ...

  10. sqlServer sa账号被锁定

    alter login sa with password = '123'  unlock, check_policy = off, check_expiration = off    一切搞定.. 1 ...