您的位置 首页 golang

golang map初始化的坑


Map nil

import (    "fmt")//panic: assignment to entry in nil maptype Param map[string]interface{}type Show struct {    Param}func main() {    s := new(Show)    //s.Param["RMB"] = 10000 //panic: assignment to entry in nil map    s.Param = map[string]interface{}{}    s.Param = Param{}    s.Param["RMB"] = 10000     fmt.Println(s)}
package mainimport "fmt"type Param map[string]interface{}type Show struct {    *Param}func main() {    s := new(Show)    // s.Param["RMB"] = 10000 //invalid operation: s.Param["RMB"] (type *Param does not support indexing)    p := Param{}    s.Param = &p    //s.Param["RMB"] = 100000 //s.Param["RMB"] (type *Param does not support indexing)    (*s.Param)["RMB"] = 100000    fmt.Println(s.Param)   }

文章来源:智云一二三科技

文章标题:golang map初始化的坑

文章地址:https://www.zhihuclub.com/1237.shtml

关于作者: 智云科技

热门文章

发表评论

您的电子邮箱地址不会被公开。

网站地图