英文:
Error "(no value) used as value" from Go compiler using oklog/run
问题
我正在处理一个oklog/run包的简单示例,并且在尝试返回错误日志时,在VS Code中遇到了这个编译错误:
log.Errorf("abnormal termination: %s", err) (no value) used as value
在group.Run的描述中,它说:
"..当所有actor都退出时,Run才会返回。Run返回第一个退出actor返回的错误。"
我想知道这是否与此有关,例如,它无法编译当前不存在的错误,因为run.Group尚未全部返回?
谢谢您可能提供的任何帮助。
代码:
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编译器的错误:
英文:
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:
答案1
得分: 5
log.Errorf
没有返回值,但你正在尝试使用return
关键字返回它。
请尝试使用以下代码:
if err := group.Run(); !errors.As(err, &run.SignalError{}) {
log.Errorf("异常终止:%s", err)
return
}
英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论