错误信息:Go编译器使用oklog/run时出现错误”(no value) used as value”。

huangapple go评论98阅读模式
英文:

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编译器的错误:

错误信息:Go编译器使用oklog/run时出现错误”(no value) used as value”。

英文:

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(&quot;abnormal termination: %s&quot;, 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 (
&quot;context&quot;
&quot;time&quot;
&quot;github.com/oklog/run&quot;
&quot;github.com/pkg/errors&quot;
log &quot;github.com/sirupsen/logrus&quot;
)
func logAForever(ctx context.Context) {
for {
select {
case err := &lt;-ctx.Done():
log.Error(err)
return
default:
log.Info(&quot;A&quot;)
time.Sleep(1 * time.Second)
}
}
}
func logBFor10Sec(ctx context.Context) {
for i := 1; i &lt; 10; i++ {
log.Info(&quot;B&quot;)
time.Sleep(1 * time.Second)
}
}
func main() {
ctx, testStopFunc := context.WithCancel(context.Background())
var group run.Group
group.Add(func() error {
log.Info(&quot;First actor added to run group. Starting execute function...&quot;)
logAForever(ctx)
return nil
}, func(error) {
log.Info(&quot;The first interrupt function was invoked.&quot;)
testStopFunc()
time.Sleep(100 * time.Millisecond)
})
group.Add(func() error {
log.Info(&quot;Second actor added to run group. Starting execute function...&quot;)
logBFor10Sec(ctx)
return nil
}, func(error) {
log.Info(&quot;The second interrupt function was invoked.&quot;)
testStopFunc()
time.Sleep(100 * time.Millisecond)
})
if err := group.Run(); !errors.As(err, &amp;run.SignalError{}) {
// return
return log.Errorf(&quot;abnormal termination: %s&quot;, err)
}
}

Error from VSCode compiler:

错误信息:Go编译器使用oklog/run时出现错误”(no value) used as value”。

答案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, &amp;run.SignalError{}) {
  log.Errorf(&quot;abnormal termination: %s&quot;, err)
  return
}

huangapple
  • 本文由 发表于 2022年7月22日 05:02:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/73072579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定