dpr26232 2016-01-25 15:08
浏览 32
已采纳

“声明且未使用”是因为Go编译器无法考虑循环?

Poor title, but I didn't know how else to describe it within the character limit.

As a learning exercise, I'm trying to write a little Go program that simulates a lottery draw. It draws six random numbers to be the winning set, then continually draws random arrays of random ints until you get that set again.

First I wrote a function which takes a channel and infinitely loops "add array of 6 random ints to the channel":

func draw(ch chan<- [6]int) {
    generator := rand.New(rand.NewSource(time.Now().UnixNano()))
    for {
        ch <- [6]int{
            generator.Intn(100),
            generator.Intn(100),
            generator.Intn(100),
            generator.Intn(100),
            generator.Intn(100),
            generator.Intn(100),
        }
    }
}

Then in main() I specify two OS threads, create a channel that can hold 250 arrays of 6 ints, and start my draw() function in a goroutine.

runtime.GOMAXPROCS(2)
ch := make(chan [6]int, 250)
go draw(ch)

Next I take a winning set (eg [4 8 15 16 23 42]) from the channel, then a 'current' set, meaning the most recent draw. I set my game_played counter to 1:

winning := <- ch
current := <- ch
games_played := 1

Here's the tricky bit.

In an infinite loop, I check if the current draw is equal to the winning draw. If it is, I print the number of games played, and break from the loop.

If it isn't, I set current to a new draw, and increment the counter. The loop should then run the if winning == current... check again over and over until there's a match.

for {
    if winning == current {
        fmt.Println(games_played)
        break
    } else {
        current := <- ch
        games_played += 1
    }
}

Here's the problem: that fourth-last line, current := <- ch, throws a compiler error, 'current declared and not used'. I want to say "Yeah, I know it's not used after that point reading downward, but it's declared inside a loop, so its value matters on the next iteration." But I can't work out how, or if I've done something stupid. I'm totally clueless about Go, obviously. But to me, thinking through it, the logic is sound. Am I messing something up?

(Note: I'm aware of the oversight that a draw of [1 2 3] won't be equal to [2 3 1], ignoring that for now.)

  • 写回答

1条回答 默认 最新

  • duan0821 2016-01-25 15:14
    关注

    The second current is in the if scope. Replace current := <- ch with current = <- ch (no colon). When using := you define a new current variable in the nested scope.

    if winning == current {
        fmt.Println(games_played)
        break
    } else {
        current := <- ch
        games_played += 1
    }
    

    is equivalent of:

    if winning == current {
        fmt.Println(games_played)
        break
    } else {
        var current int[6] // you don't want this, as it shadows your
                           // current form the outher scope
        current = <- ch // you want only this guy
        games_played += 1
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值