普通网友 2017-03-01 10:49
浏览 53
已采纳

为什么我们不能声明一个映射并在const之后填充它?

Just why... why does this generate an error? Is it because it reallocates the map to allow the extension or just because the compiler parser is not meant to handle these cases?

const (
        paths = &map[string]*map[string]string {
            Smith: {
                "theFather": "John",
            },
        }
        paths["Smith"]["theSon"] = paths["Smith"]["theFather"] + " Junior"
)
  • 写回答

1条回答 默认 最新

  • dongquanyu5816 2017-03-01 11:02
    关注

    Constants are called constants for a reason: you expect them to be just that: constant.

    The type defines what operations you may perform on a value of that type. The map type in Go is not constant, you can change its key-value pairs after its creation, so you cannot have map constants.

    Your "palette" to choose constants from is defined in the Spec: Constants:

    There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants.

    See related question: Declare a constant array

    Workaround is simple: declare it to be a variable instead of a constant.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 传染病最优控制问题,控制为什么没起作用
  • ¥15 请问一个软件连接不上服务器了怎么办呀
  • ¥15 ue5.3 pico打包失败 求帮助
  • ¥15 请教,这是用chatgpt写的CAD LISPD脚本,需求是画一个由双直线组成的矩形
  • ¥50 微信小程序 成功包奶茶啊
  • ¥15 计算机博弈的六子棋代码
  • ¥15 在Fetch API中传送的参数为何不起作用?
  • ¥15 问题遇到的现象和发生背景 360导航页面千次ip是20元,但是我们是刷量的 超过100ip就不算量了,假量超过100就不算了 这是什么逻辑呢 有没有人能懂的 1000元红包感谢费
  • ¥30 计算机硬件实验报告寻代
  • ¥15 51单片机写代码,要求是图片上的要求,请大家积极参与,设计一个时钟,时间从12:00开始计时,液晶屏第一行显示time,第二行显示时间