doufu1950 2016-01-06 15:32
浏览 53
已采纳

转到如何正确使用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?

  • 写回答

2条回答 默认 最新

  • duanqian1888 2016-01-06 15:40
    关注

    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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿