dongzhuang2030 2018-12-30 21:04
浏览 33

如果我将golang的增量乘以for循环不增加

I was assuming I can use any operator to increment the incrementing variable in for loop. Looks like it's not the case. Following code loops for ever.

import (
    "fmt"
)

func main() {
    for i:=0; i<10; i=i*2{
    fmt.Println(i)
    }
}

go playground

Following code works fine.

import (
    "fmt"
)

func main() {
    for i:=0; i<10; i=i+2{
    fmt.Println(i)
    }
}
  • 写回答

1条回答 默认 最新

  • douluokuang7184 2018-12-30 21:13
    关注

    Your loop starts at i := 0, so you are just continuously doing i := 0 * 2, so you get an infinite loop (as you should), since 0 < 10 and i never actually gets bigger.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化