默认情况下,除了我想要uint64之外,Go默认使用int作为迭代器.我无法找到一种方法来改变Go中for循环迭代器的类型.有没有办法与for语句一起内联?当我尝试在循环中执行某些操作时,默认类型的int会导致问题,例如mod操作(%).
func main() {
var val uint64 = 1234567890
for i:=1; i<val; i+=2 {
if val%i==0 {
}
}
}
./q.go:7: invalid operation: i < val (mismatched types int and uint64)
./q.go:8: invalid operation: val % i (mismatched types uint64 and int)
Run Code Online (Sandbox Code Playgroud)
the*_*tem 26
你的意思是这样的?
for i, val := uint64(1), uint64(1234567890); i<val; i+=2 {
// your modulus operation
}
Run Code Online (Sandbox Code Playgroud)
http://play.golang.org/p/yAdiJu4pNC
| 归档时间: |
|
| 查看次数: |
4525 次 |
| 最近记录: |