我正在编写一个简单的oklog/run包示例,在try 返回错误日志(log)时,我在VS代码中看到了这个编译错误:
log.Errorf("abnormal termination: %s", err) (no value) used as value
在组的描述中.运行它说
".Run仅在所有参与者退出时返回.Run返回第一个退出参与者返回的错误."
我想知道这是否与此有关,比如它无法使用当前不存在的错误进行编译,因为运行.团队还没有全部回来?
谢谢你的帮助.
代码:
package main
import (
"context"
"time"
"github.com/oklog/run"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
func logAForever(ctx context.Context) {
for {
select {
case err := <-ctx.Done():
log.Error(err)
return
default:
log.Info("A")
time.Sleep(1 * time.Second)
}
}
}
func logBFor10Sec(ctx context.Context) {
for i := 1; i < 10; i++ {
log.Info("B")
time.Sleep(1 * time.Second)
}
}
func main() {
ctx, testStopFunc := context.WithCancel(context.Background())
var group run.Group
group.Add(func() error {
log.Info("First actor added to run group. Starting execute function...")
logAForever(ctx)
return nil
}, func(error) {
log.Info("The first interrupt function was invoked.")
testStopFunc()
time.Sleep(100 * time.Millisecond)
})
group.Add(func() error {
log.Info("Second actor added to run group. Starting execute function...")
logBFor10Sec(ctx)
return nil
}, func(error) {
log.Info("The second interrupt function was invoked.")
testStopFunc()
time.Sleep(100 * time.Millisecond)
})
if err := group.Run(); !errors.As(err, &run.SignalError{}) {
// return
return log.Errorf("abnormal termination: %s", err)
}
}
VSCode编译器出错: