initpy / go-book

A book for learning the Go Programming Language
http://go-book.appspot.com/
115 stars 30 forks source link

Functions as Data section: change var name from "key" to "index" #56

Open kkruups opened 8 years ago

kkruups commented 8 years ago

Hi Yuuta,

In section "Functions As Data" you wrote the code below. Since activity is a slice and not a map you should refrain from using the word variable name "key", instead use variable name "index", since keys are associated with maps not slices or arrays.

Changing the name will make your code much easier to read and understand.

You wrote:

func compose(p phrases, a []activity) (func()){
    return func(){
        for key, value := range a{
            fmt.Print(p[value])
            if key == len(a)-1{
                fmt.Println(".")
            } else {
                fmt.Print(" and ")
            }
        }
    }
}

Should be corrected in following manner (see use of variable name "index" in place of "key")

func compose(p phrases, a []activity) (func()){
    return func(){
        for index, value := range a{
            fmt.Print(p[value])
            if index == len(a)-1{
                fmt.Println(".")
            } else {
                fmt.Print(" and ")
            }
        }
    }
}