gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:06 30 4
gpt4 key购买 nike

只是为什么……为什么会产生错误?是因为它重新分配了映射以允许扩展,还是仅仅因为编译器解析器并不打算处理这些情况?

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

最佳答案

Constants被称为常量是有原因的:您希望它们就是这样:常量

类型 定义了您可以对该类型的值执行的操作。 map Go 中的类型不是常量,你可以在它创建后更改它的键值对,所以你不能有 map 常量。

您选择常量的“调色板”在规范中定义:常量:

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

参见相关问题:Declare a constant array

解决方法很简单:将其声明为变量而不是常量。

关于go - 为什么我们不能在 const 中声明一个映射并在后面填充它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42529691/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com