如何在Golang中声明常量映射?
我试图在Go中声明为常量,但它抛出了一个错误。有谁能帮我在Go中声明常量的语法吗 这是我的代码:如何在Golang中声明常量映射?,go,Go,我试图在Go中声明为常量,但它抛出了一个错误。有谁能帮我在Go中声明常量的语法吗 这是我的代码: const romanNumeralDict map[int]string = { 1000: "M", 900 : "CM", 500 : "D", 400 : "CD", 100 : "C", 90 : "XC", 50 : "L", 40 : "XL", 10 : "X", 9 : "IX", 5 : "V", 4 : "IV
const romanNumeralDict map[int]string = {
1000: "M",
900 : "CM",
500 : "D",
400 : "CD",
100 : "C",
90 : "XC",
50 : "L",
40 : "XL",
10 : "X",
9 : "IX",
5 : "V",
4 : "IV",
1 : "I",
}
这就是错误所在
# command-line-arguments
./Roman_Numerals.go:9: syntax error: unexpected {
你的语法不正确。要制作文字映射(作为伪常量),可以执行以下操作:
var romanNumeralDict = map[int]string{
1000: "M",
900 : "CM",
500 : "D",
400 : "CD",
100 : "C",
90 : "XC",
50 : "L",
40 : "XL",
10 : "X",
9 : "IX",
5 : "V",
4 : "IV",
1 : "I",
}
在func
中,您可以这样声明它:
romanNumeralDict := map[int]string{
...
在围棋中没有常数映射。可以找到更多信息
您可以模拟带有闭包的地图:
package main
import (
"fmt"
)
// http://stackoverflow.com/a/27457144/10278
func romanNumeralDict() func(int) string {
// innerMap is captured in the closure returned below
innerMap := map[int]string{
1000: "M",
900: "CM",
500: "D",
400: "CD",
100: "C",
90: "XC",
50: "L",
40: "XL",
10: "X",
9: "IX",
5: "V",
4: "IV",
1: "I",
}
return func(key int) string {
return innerMap[key]
}
}
func main() {
fmt.Println(romanNumeralDict()(10))
fmt.Println(romanNumeralDict()(100))
dict := romanNumeralDict()
fmt.Println(dict(400))
}
您可以通过多种不同的方式创建常量:
const myString = "hello"
const pi = 3.14 // untyped constant
const life int = 42 // typed constant (can use only with ints)
还可以创建枚举常量:
const (
First = 1
Second = 2
Third = 4
)
您不能创建映射、数组的常量,它是用以下格式编写的:
Go中的常数就是那个常数。它们是在编译时创建的
时间,即使在函数中定义为局部变量,也只能
数字、字符(符文)、字符串或布尔值。因为
编译时限制,定义它们的表达式必须是
常量表达式,可由编译器计算。例如,如上所述,1不可能将映射定义为常量。 但是您可以声明一个全局变量,它是一个包含映射的结构 初始化过程如下所示:
var romanNumeralDict = struct {
m map[int]string
}{m: map[int]string {
1000: "M",
900: "CM",
//YOUR VALUES HERE
}}
func main() {
d := 1000
fmt.Printf("Value of Key (%d): %s", d, romanNumeralDict.m[1000])
}
正如上面所建议的那样,使用函数,在我看来,该函数更有意义,并且使您能够方便地使用映射类型,而不必使用函数包装器:
// romanNumeralDict returns map[int]string dictionary, since the return
// value is always the same it gives the pseudo-constant output, which
// can be referred to in the same map-alike fashion.
var romanNumeralDict = func() map[int]string { return map[int]string {
1000: "M",
900: "CM",
500: "D",
400: "CD",
100: "C",
90: "XC",
50: "L",
40: "XL",
10: "X",
9: "IX",
5: "V",
4: "IV",
1: "I",
}
}
func printRoman(key int) {
fmt.Println(romanNumeralDict()[key])
}
func printKeyN(key, n int) {
fmt.Println(strings.Repeat(romanNumeralDict()[key], n))
}
func main() {
printRoman(1000)
printRoman(50)
printKeyN(10, 3)
}
这实际上在编译时在函数体外部抛出一个
非声明语句。怎么会这样?@AlessandroDiaferia我没有犯这样的错误。如何使用它?@AlessandroDiaferia在这种情况下尝试使用var RomanDictmap[int]string=map[int]string{…}
。@alediaferia如果在函数外部使用:=
,就会出现错误。什么是“伪常量”?(TestMostSoldRecommender?)这实际上是一个可能的解决方案。然而,由于作者没有解释任何内容(并且将所有内容都放在一个命名奇怪的测试用例中),所以答案看起来不正确。逻辑是:(1)创建一个匿名函数(2)匿名函数封装map
(3)匿名函数返回“一个接受int并返回字符串的函数”(4)返回的函数使用map
(5)进行int->string映射立即执行匿名函数,并将返回的函数分配给变量。这个变量可以像函数一样使用,效果就像一个映射。。。为什么是数学呢?那么,Sin不是constexpr函数!您的陈述是正确的,但问题是关于创建一个恒定的映射。@jzer7您能解释一下为什么我的答案是不相关的吗?他问我如何创造一些东西,我告诉他这是不可能的。解释了什么是可能的,并引用了文档中的一句话,说明了为什么不可能做他想做的事情。为什么不让地图成为全局变量呢?为什么要将其包装在结构中?这不会使映射保持不变,您仍然可以执行romanumericandict.m[1000]=“New value”
golang不允许常量映射。这是一个很好的解释