How to declare a constant map in Golang?

前端 未结 5 1824
清歌不尽
清歌不尽 2021-01-30 15:30

I am trying to declare to constant in Go, but it is throwing an error. Could anyone please help me with the syntax of declaring a constant in Go?

This is my code:

5条回答
  •  鱼传尺愫
    2021-01-30 15:58

    Your syntax is incorrect. To make a literal map (as a pseudo-constant), you can do:

    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",
    }
    

    Inside a func you can declare it like:

    romanNumeralDict := map[int]string{
    ...
    

    And in Go there is no such thing as a constant map. More information can be found here.

    Try it out on the Go playground.

提交回复
热议问题