如何在此简单程序中修复“已声明但未使用”的编译器错误?

I am trying to learn Go. I really don't understand why the compiler is saying that I am not using a variable. It seems to me that I am using the variable as an argument to Println.

My textbook states:

In this for loop i represents the current position in the array and value is the same as x[i]

package main

import "fmt"

func main() {

    x := [5]float64{ 1,2,3,4,5 }
    i := 0
    var total float64 = 0

    for i, value := range x {
        total += value
        fmt.Println(i, value)
    }   
    fmt.Println("Average:", total / float64(len(x)))
}

Output on OS X:

go run main.go 
# command-line-arguments
./main.go:8: i declared and not used

Surely this fmt.Println(i, value) is using the variable i?

How to fix the compiler message?

Remove the outer i from your program:

package main

import "fmt"

func main() {

    x := [5]float64{1, 2, 3, 4, 5}
    var total float64 = 0

    for i, value := range x {
        total += value
        fmt.Println(i, value)
    }
    fmt.Println("Average:", total/float64(len(x)))
}

Surely this fmt.Println(i, value) is using the variable i?

Yes, but the one you're defining inside the for loop. (note the :=), here:

for i, value := range x
    ^        ^

The outer variable i is never used.