我正在研究oklog/run包的一個簡單示例,并且在嘗試返回錯誤日志時在 VS Code 中看到此編譯錯誤:log.Errorf("abnormal termination: %s", err) (no value) used as value在 group.Run 的描述中說“..Run 僅在所有 actor 退出時返回。Run 返回第一個退出的 actor 返回的錯誤。”我想知道這是否與它有關,比如它無法使用當前不存在的錯誤進行編譯,因為 run.Group 尚未全部返回?感謝您提供的任何幫助。代碼:package mainimport ( "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) }}
1 回答

慕容森
TA貢獻1853條經驗 獲得超18個贊
log.Errorf不返回任何值,但您正試圖通過return關鍵字返回它。
請嘗試使用此代碼:
if err := group.Run(); !errors.As(err, &run.SignalError{}) {
log.Errorf("abnormal termination: %s", err)
return
}
- 1 回答
- 0 關注
- 107 瀏覽
添加回答
舉報
0/150
提交
取消