gocompiler-errorsgo-context

Error "(no value) used as value" from Go compiler using oklog/run


I am working on a simple example of oklog/run package, and am seeing this compilation error in VS Code when trying to return a log of the error:

log.Errorf("abnormal termination: %s", err) (no value) used as value

In the description of group.Run it says

"..Run only returns when all actors have exited. Run returns the error returned by the first exiting actor."

I’m wondering if that has something to do with it, like it can’t compile with the currently non-existent error because the run.Group hasn't all returned yet?

Thanks for any help you may have.

Code:

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)
    }

}

Error from VSCode compiler:

enter image description here


Solution

  • log.Errorf returns no value, but you are trying to return it via the return keyword.

    Try this code instead:

    if err := group.Run(); !errors.As(err, &run.SignalError{}) {
      log.Errorf("abnormal termination: %s", err)
      return
    }