I'm not sure what's happening here.
I'm working on Project Euler #8 and have come up with the following function to handle getting the product of 5 digits:
func fiveDigitProduct(n int) int {
localMax := n + 5
product := 1
for n; n < localMax; n++ {
f, _ := strconv.Atoi(input[n])
product *= f
}
return product
}
However, I keep getting the warning "n evaluated but not used". I have no idea why this is happening.
The InitStmt
(initialization statement) of your For Statement isn't actually doing any initialization. You're asking the compiler to evaluate n
but not do anything with it, which is what the compiler is complaining about. Since you don't need to initialize n
for you loop, just do:
for ; n < localMax; n++ {
The initialization part of a for loop as also the post part (where you increment n) needs to be a simple statement (and just "n" is not a statement, it'll be an expression, so what you are trying to do is syntactically incorrect). If you make the init part n = n or n := n (a new variable declaration within the scope of the for loop), it'll be a valid statement and will work. As suggested in the above posts, since you don't intend to do anything in the init part it's best to leave it out.