GO:使用for循环将其添加到地图中

huangapple go评论78阅读模式
英文:

GO: Add to map using for loop

问题

我一直在尝试在Go语言中使用for循环向地图(map)添加元素,但是我一直无法成功。有人可以给出一个简单的示例来说明如何实现吗?

谢谢!

英文:

I have been trying to add to a map using a for loop in golang but I haven't been able to do so. Could anybody give a simple example of how it could be done?

Thank you!

答案1

得分: 2

(从评论转为回答。)

myMap := make(map[int]string)
for i := 0; i < 3; i++ {
	myMap[i] = fmt.Sprintf("迭代 %d;", i)
}
英文:

(Moving from a comment to an answer.)

myMap := make(map[int]string)
for i := 0; i &lt; 3; i++ {
	myMap[i] = fmt.Sprintf(&quot;iteration %d;&quot;, i)
}

huangapple
  • 本文由 发表于 2014年1月20日 02:57:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/21221145.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定