For loop Increment with a step not working

自作多情 提交于 2020-02-05 04:32:05

问题


The following Go program fails to compile

package main

import (
"fmt"
)

func main() {
var celcius int
for i := 0; i <= 300; i + 20 {
    celcius = 5 * (i - 32) / 9
    fmt.Printf("%d \t %d\t \n", i, celcius)
}
}

The Error message is "i + 20 evaluated but not used" . How to give a step increment in golang for loop


回答1:


The compiler is complaining that the result of the expression i + 20 is not used. One fix is to assign result to i:

for i := 0; i <= 300; i = i + 20 {

A shorter and more idiomatic approach is to use +=:

for i := 0; i <= 300; i += 20 {


来源:https://stackoverflow.com/questions/39188542/for-loop-increment-with-a-step-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!