duanji2002 2018-07-05 23:14
浏览 27
已采纳

错误发生在Go-lang中事件的逻辑顺序上

I am having a somewhat inexplicable error in go where I encounter an error at a line which the program, in theory, should not be executing at that moment.

func exampleFunction() { 
  //cycleCount starts as 1, layerDif = 4 , composition is an int array with 5 entries  
  if cycleCount <= layerDif {
    fmt.Println("Evaluating if")
    fmt.Println("layerDif", layerDif, "cycleCount", cycleCount)
    for i := 0; i < composition[cycleCount]; i++ { //this is line 68
      //... random code
      cycleCount++
      fmt.Println("cycle++",cycleCount)
      exampleFunction() // this is line 72
    }
  } else {

    fmt.Println("Evaluating else")

    //... random code

    fmt.Println("Argyle")

    for i := 0; i < layerDif -1; i++ {
      fmt.Println("Sock", i)
      //... random code
    }

    //... random code

  }
}

This code results in an output of:

Evaluating if
layerDif 4 cycleCount 1
cycles 2
Evaluating if
layerDif 4 cycleCount 2
cycles 3
Evaluating if
layerDif 4 cycleCount 3
cycles 4
Evaluating if
layerDif 4 cycleCount 4
cycles 5
Evaluating else
panic: runtime error: index out of range

goroutine 1 [running]:
main.exampleFunction()
    /home/name/Go/Predict/src/main/Filename.go:68 +0x5c1
main.exampleFunction()
    /home/name/Go/Predict/src/main/Filename.go:72 +0x25e
main.exampleFunction()
    /home/name/Go/Predict/src/main/Filename.go:72 +0x25e
main.exampleFunction()
    /home/name/Go/Predict/src/main/Filename.go:72 +0x25e
...some other output which doesn't matter
exit status 2

I'm at somewhat of a loss for words, as the line at which the error occurs is listed as 68. However, just before this output is given, it prints "Evaluating else", an output which should be given only once line 68 is done being evaluated (The recursive function has stopped). The second mystery is why the for loop following "Evaluating else" is never executed (And should have printed a series of outputs with "Sock" in them). This is somewhat bewildering and I can't exactly pinpoint the cause of this error. Why is this occurring?

  • 写回答

1条回答 默认 最新

  • du0923 2018-07-13 00:32
    关注

    I was able to replicate your error with the following code:

    package main
    
    import "fmt"
    
    var cycleCount int
    var layerDif int
    var composition [5]int
    
    func main() {
        cycleCount = 1
        layerDif = 4
        composition = [5]int{0, 1, 2, 3, 4}
        exampleFunction()
    }
    
    func exampleFunction() {
        if cycleCount <= layerDif {
            fmt.Println("Evaluating if")
            fmt.Println("layerDif", layerDif, "cycleCount", cycleCount)
            for i := 0; i < composition[cycleCount]; i++ { //this is line 68
                cycleCount++
                fmt.Println("cycle++", cycleCount)
                exampleFunction() // this is line 72
            }
        } else {
            fmt.Println("Evaluating else")
            fmt.Println("Argyle")
            for i := 0; i < layerDif-1; i++ {
                fmt.Println("Sock", i)
            }
        }
    }
    

    Outputs:

    Evaluating if
    layerDif 4 cycleCount 1
    cycle++ 2
    Evaluating if
    layerDif 4 cycleCount 2
    cycle++ 3
    Evaluating if
    layerDif 4 cycleCount 3
    cycle++ 4
    Evaluating if
    layerDif 4 cycleCount 4
    cycle++ 5
    Evaluating else
    Argyle
    Sock 0
    Sock 1
    Sock 2
    panic: runtime error: index out of range
    
    goroutine 1 [running]:
    main.exampleFunction()
        /Users/daniel/Desktop/main.go:20 +0x38c
    main.exampleFunction()
        /Users/daniel/Desktop/main.go:23 +0x21a
    main.exampleFunction()
        /Users/daniel/Desktop/main.go:23 +0x21a
    main.exampleFunction()
        /Users/daniel/Desktop/main.go:23 +0x21a
    main.main()
        /Users/daniel/Desktop/main.go:13 +0x65
    exit status 2
    

    The problem with this code (and I guess with your code, too) is the elements of array composition. The for loop in line 68 loops from 0 to composition[cycleCount], that means:

    i = 0
    i = composition[CycleCount] == composition[1] == 1
    // CycleCount is incremented, so:
    i = composition[CycleCount] == composition[2] == 2
    // and so on
    i = 3
    i = 4
    i = 5
    // at this point, else gets evaluated and you see the results of the respective for loop printed
    

    Now, CycleCount will be incremented once more and is now 6, but composition has less than 6 elements and you get a runtime panic.

    EDIT: I just realized that @matb already mentioned this in his comment. He also suggests the (in my opinion) correct solution, i.e. to check len(composition) in your for loop condition.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗