亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 oklog/run 來自 Go 編譯器的錯誤“(無值)用作值”

使用 oklog/run 來自 Go 編譯器的錯誤“(無值)用作值”

Go
浮云間 2023-01-03 16:47:36
我正在研究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

}


查看完整回答
反對 回復 2023-01-03
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號