如何声明常量贴图

时间:2013-08-20 18:15:31

标签: 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",
  1   : "I",
}

这是错误

# command-line-arguments
./Roman_Numerals.go:9: syntax error: unexpected {

5 个答案:

答案 0 :(得分:119)

您的语法不正确。要制作文字地图(作为伪常量),您可以执行以下操作:

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{
...

在Go中没有恒定的地图。可以找到更多信息here

Try it out on the Go playground.

答案 1 :(得分:17)

您可以通过多种不同方式创建常量:

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
)

您不能创建地图,数组的常量,它是用effective go

编写的
  

Go中的常量只是常量。它们是在编译时创建的   时间,即使在函数中被定义为本地人,也只能是   数字,字符(符文),字符串或布尔值。因为   编译时限制,定义它们的表达式必须是   常量表达式,可由编译器评估。例如,1 <&lt; 3   是一个常量表达式,而math.Sin(math.Pi / 4)则不是因为   函数调用math.Sin需要在运行时发生。

答案 2 :(得分:8)

您可以使用闭包来模拟地图:

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))
}

Try it on the Go playground

答案 3 :(得分:1)

正如上面Siu Ching Pong -Asuka Kenji所建议的那样,我觉得这个函数更有意义,让你在没有函数包装的情况下方便地使用地图类型:

   // 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)
        }

Try this at play.golang.org.

答案 4 :(得分:-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])
}
相关问题