有没有办法在Golang中迭代一系列整数? [英] Is there a way to iterate over a range of integers in Golang?

查看:134
本文介绍了有没有办法在Golang中迭代一系列整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Golang的范围可以迭代地图和切片,但我想知道是否有一种方法可以遍历一系列数字,例如

  for i:= range [1..10] {
fmt.Println(i)
}

或者有没有一种方法可以像Goy那样表示Go中的整数范围?

解决方案

你可以,也应该写一个for循环。简单明显的代码就是Go方式。

  for i:= 1;我<= 10; i ++ {
fmt.Println(i)
}


Golang's range can iterate over maps and slices, but I was wondering if there is a way to iterate over a range of numbers, something like this

for i := range [1..10] {
    fmt.Println(i)
}

or is there a way to represent range of integers in Go like how ruby does?

解决方案

You can, and should, just write a for loop. Simple, obvious code is the Go way.

for i := 1; i <= 10; i++ {
    fmt.Println(i)
}

这篇关于有没有办法在Golang中迭代一系列整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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