golang 中对 map 类型中的 struct 赋值报错

type s  struct{
name string
age int
}
func main(){
a := map[string]s{
"tao":{
"li",
18,},
} fmt.Println(a["tao"].age)
a["tao"].age += 1 //注释后可以执行
fmt.Println(a["tao"].age)
}

./test.go:16:15: cannot assign to struct field a["tao"].age in map

原因是 map 元素是无法取址的,也就说可以得到 a["tao"], 但是无法对其进行修改。

解决办法:使用指针的map

type s  struct{
name string
age int
}
func main(){
a := map[string]*s{
"tao":{
"li",
,},
} fmt.Println(a["tao"].age)
a["tao"].age +=
fmt.Println(a["tao"].age)
}

cannot assign to struct field xxx in map的更多相关文章

  1. can't assign to struct fileds in map

    原文: https://haobook.readthedocs.io/zh_CN/latest/periodical/201611/zhangan.html --------------------- ...

  2. post请求(headers里有属性)报错:Request header field xxx is not allowed by Access-Control-Allow-Headers in preflight response

    post 请求,headers里有属性(xxx).请求时报错: XMLHttpRequest cannot load <url>. Request header field xxx is ...

  3. 微信小程序 报错:Setting data field "xxx" to undefined is invalid

    通过网络请求获取的数据,当返回的数据没有xxx(变量名)这个变量时,此时xxx是undefined 若使用setData进行赋值,则会报如下的错误: Setting data field " ...

  4. Android 找不到所标识的资源 java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XX/R$id

    报错: java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XXX/R$id; or its supercl ...

  5. java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XX/R$id; or its superclasses

    报错: 当启动一个页面的时候报错: java.lang.NoSuchFieldError: No static field XXX of type I in class Lcom/XXX/R$id; ...

  6. 匿名字段 内嵌结构体 interface作为struct field 匿名接口

    interface作为struct field,谈谈golang结构体中的匿名接口 - Go语言中文网 - Golang中文社区 https://studygolang.com/articles/19 ...

  7. Can not set int field xxx to java.lang.Long 错误

    Can not set int field xxx to java.lang.Long 错误 这个错误其实是因为Java程序和MySQL表中字段的属性匹配不一致 我的报错是Can not set ja ...

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

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

  9. hive发杂数据结构的使用,struct,array,map

    一个简单介绍http://jimi68.iteye.com/blog/980573 一般的复杂介绍:http://songpo-ath-taobao-com.iteye.com/blog/140513 ...

随机推荐

  1. zabbix发送报警的脚本

    zabbix报警媒介:自定义脚本Custom alertscripts 邮件报警准备工作:安装sendEmail zabbix-server 的 配置文件 /etc/zabbix/zabbix_ser ...

  2. python-函数3(全局变量与局部变量)

    python-函数3(全局变量与局部变量) 全局变量与局部变量 school = "goy edu" 全局变量,在最上面定义的 def change_name(name): glo ...

  3. AIX查看系统日志

    1.查看系统启动日志 在AIX中可以使用alog命令来查看系统日志.   启动日志: /var/adm/ras/bootlog /var/adm/ras/bosinstlog /var/adm/ras ...

  4. Centos7下常用配置命令

    1.禁用ipv6 Step 1: add this rule in /etc/sysctl.conf : net.ipv6.conf.all.disable_ipv6=1 Step 2: add th ...

  5. D Merge Equals Educational Codeforces Round 42 (Rated for Div. 2) (STL )

    D. Merge Equals time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...

  6. u-boot v2018.01 启动流程分析 简单版(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/weixin_39655765/artic ...

  7. java map 根据 map的value值进行排序

    //根据销量排行查询 public void queryGoodsByHotCount(){ //将map集合键和值封装到entry对象中 然后转换成set集合 Set<Entry<Int ...

  8. 有关pip报错的问题

    错误消息:“Fatal error in launcher: Unable to create process using '"' 解决办法: python3解决方案:python3 -m ...

  9. #python#return和print的一些理解

    https://www.jianshu.com/p/18a6c0c76438 代码 (1) ++++++++++++++++++++++++++++++++++ x = 1y = 2def add ( ...

  10. 自己用ul模拟实现下拉多选框,

    模拟实现下拉多选框 效果如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...