转到如何正确使用for…range循环

At the moment I have a go program that contains the following code.

package main

import "time"
import "minions/minion"

func main() {
    // creating the slice
    ms := make([]*minion.Minion, 2)

    //populating the slice and make the elements start doing something
    for i := range ms  {
        m := &ms[i]
        *m = minion.NewMinion()
        (*m).Start()
    }

    // wait while the minions do all the work
    time.Sleep(time.Millisecond * 500)

    // make the elements of the slice stop with what they were doing
    for i := range ms {
        m := &ms[i]
        (*m).Stop()
    }
}

Here NewMinion() is a constructor that returns a *minion.Minion

The code works perfectly, but having to write m := &ms[i] every time I use a for ... range loop seems to me like there should be a code writer friendlier way to tackle this problem.

Ideally I'd like something like the following to be possible (using the made up &range tag):

package main

import "time"
import "minions/minion"

func main() {
    // creating the slice
    ms := make([]*minion.Minion, 2)

    //populating the slice and make the elements start doing something
    for _, m := &range ms  {
        *m = minion.NewMinion()
        (*m).Start()
    }

    // wait while the minions do all the work
    time.Sleep(time.Millisecond * 500)

    // make the elements of the slice stop with what they were doing
    for _, m := &range ms {
        (*m).Stop()
    }
}

Unfortunately, this is not a language feature as of yet. Any considerations on what would be the nicest way remove the m := &ms[i] from the code? Or is there no way yet that takes less effort to write than this?

Your first example is a slice of pointers, you don't need to take the address of the pointers in the slice and then dereference the pointers each time. More idiomatic Go would look like (edited slightly to run in the playground without the "minion" package):

http://play.golang.org/p/88WsCVonaL

// creating the slice
ms := make([]*Minion, 2)

//populating the slice and make the elements start doing something
for i := range ms {
    ms[i] = NewMinion(i)
    ms[i].Start()

    // (or equivalently) 
    // m := MewMinion(i)
    // m.Start()
    // ms[i] = m
}

// wait while the minions do all the work
time.Sleep(time.Millisecond * 500)

// make the elements of the slice stop with what they were doing
for _, m := range ms {
    m.Stop()
}

This is all wrong.

There is absolutely no need to take the address of a pointer in your code. ms is a slice of pointers and you constructor returns a pointer so just assign i directly:

for i := range ms  {
    ms[i] = minion.NewMinion()
    ms[i].Start()
}

Dead simple.