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 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口