如何在调试过程中不单步执行时停止时间?
How to stop the time when not stepping through lines during debugging?
当调试一个使用 say context.WithTimeout
的程序时,当你没有逐行执行时,时钟会一直滴答作响,所以在你调试依赖于给定上下文的代码片段之前,该上下文已经完成,因此您感兴趣的调试代码段不会执行。例如,在下面的代码片段中,我必须增加 timestamp
值才能单步执行 do()
和 retry()
,否则会在我执行此操作之前达到超时:
package main
import (
"context"
"fmt"
"math/rand"
"time"
)
const success = 0.1
const timeframe = time.Microsecond * 2
func main() {
ctx, cancel := context.WithTimeout(context.Background(), timeframe)
do(ctx, cancel)
}
func do(ctx context.Context, cancel context.CancelFunc) {
defer retry(ctx, cancel)
select {
case <-ctx.Done(): // timeout will be reached before i reach this line
panic("fail")
default:
if rand.Float64() < success {
cancel()
fmt.Println("success")
return
} else {
fmt.Println("fail")
}
}
func retry(ctx context.Context, cancel context.CancelFunc) {
r := recover()
if r == nil {
do(ctx, cancel)
}
}
我没怎么用英语来谈论编程和技术,所以请随时要求重新措辞。
How to stop the time when not stepping through lines during debugging?
你根本做不到。
您要么必须将超时增加到可以进行手动调试的程度,要么不使用调试器。
您可以使用 中解释的构建标记技巧。
基本上创建 2 个文件,一个用于保存正常超时值,另一个用于在 运行 under delve 时保存更长的超时值。
// +build delve
package main
import "time"
const Timeframe = 10 * time.Hour
// +build !delve
package main
import "time"
const Timeframe = 2 * time.Microsecond
调用 delve 时使用 --build-flags='-tags=delve'
选择正确的文件。
当调试一个使用 say context.WithTimeout
的程序时,当你没有逐行执行时,时钟会一直滴答作响,所以在你调试依赖于给定上下文的代码片段之前,该上下文已经完成,因此您感兴趣的调试代码段不会执行。例如,在下面的代码片段中,我必须增加 timestamp
值才能单步执行 do()
和 retry()
,否则会在我执行此操作之前达到超时:
package main
import (
"context"
"fmt"
"math/rand"
"time"
)
const success = 0.1
const timeframe = time.Microsecond * 2
func main() {
ctx, cancel := context.WithTimeout(context.Background(), timeframe)
do(ctx, cancel)
}
func do(ctx context.Context, cancel context.CancelFunc) {
defer retry(ctx, cancel)
select {
case <-ctx.Done(): // timeout will be reached before i reach this line
panic("fail")
default:
if rand.Float64() < success {
cancel()
fmt.Println("success")
return
} else {
fmt.Println("fail")
}
}
func retry(ctx context.Context, cancel context.CancelFunc) {
r := recover()
if r == nil {
do(ctx, cancel)
}
}
我没怎么用英语来谈论编程和技术,所以请随时要求重新措辞。
How to stop the time when not stepping through lines during debugging?
你根本做不到。
您要么必须将超时增加到可以进行手动调试的程度,要么不使用调试器。
您可以使用
基本上创建 2 个文件,一个用于保存正常超时值,另一个用于在 运行 under delve 时保存更长的超时值。
// +build delve
package main
import "time"
const Timeframe = 10 * time.Hour
// +build !delve
package main
import "time"
const Timeframe = 2 * time.Microsecond
调用 delve 时使用 --build-flags='-tags=delve'
选择正确的文件。