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
", i, celcius)
}
}
The Error message is "i + 20 evaluated but not used" . How to give a step increment in golang for loop
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 {