I found a bug when I try to debug the golang code below:
package main
import "log"
type SA struct {
dataint int
datastr string
datasi []int
}
type SB struct {
}
func (sb *SB) sbfun(sa SA) {
}
func main() {
log.Println(`start`)
var sb *SB = nil //always nil
if sb != nil { // always false
log.Println(`flag 1`)
sb.sbfun(SA{}) // I set breakpoint here
}
log.Println(`end`)
}
I set the breakpoint and debug(using gdb or Delve), but it always interrupted there,
and no “flag 1” appeared on console, can anyone detail why such bug with this code? can anyone try to debug this code ?
many thanks.
chinese:
我发现了一个golang在debug时的BUG,对不起,我英文不好,请看以下代码:
package main
import "log"
type SA struct {
dataint int
datastr string
datasi []int
}
type SB struct {
}
func (sb *SB) sbfun(sa SA) {
}
func main() {
log.Println(`start`)
var sb *SB = nil //总是为nil
if sb != nil { // 总是为false
log.Println(`flag 1`)
sb.sbfun(SA{}) // 我在这里设置断点
}
log.Println(`end`)
}
I wouldn’t say that you’ve found a bug, but more a confusing behavior.
The block where you set the breakpoint was optimized away, because it cannot be reached. The breakpoint was probably set on the nearest bit of code that still exists, which would be log.Println(“end”). You could reasonably ask the Delve maintainers why it is doing that, it might be reasonable to hope that a debugger would say, “warning: line X does not exist, moving breakpoint to line X+2”.
…
log.Println(start)
if rand.Intn(5)>10 { // always false
log.Println(flag 1)
sb.sbfun(SA{}) // I set breakpoint here
}
…
debugger still interrupted there , and I can’t see “start” flag ,why? why i can’t see “start” when debugger interrupted there