英文:
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 < 3; i++ {
myMap[i] = fmt.Sprintf("iteration %d;", i)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论