如何在Golang中声明常量映射? [英] How to declare a constant map in Golang?

查看:40
本文介绍了如何在Golang中声明常量映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Go中声明为常量,但是它引发了错误.有人可以在Go中声明常量的语法帮助我吗?

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?

这是我的代码:

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 {

推荐答案

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

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

func 内,您可以像这样声明它:

Inside a func you can declare it like:

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

在Go中,没有常量映射之类的东西.可以在 此处 中找到更多信息.

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

在Go游乐场试用.

这篇关于如何在Golang中声明常量映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆