GoLang项目CMD命令有奇怪的行为

  • 本文关键字:项目 CMD 命令 GoLang go
  • 更新时间 :
  • 英文 :


我正在为Rosetta-API做一个GoLang项目。有一个exec的实现。命令

代码:

func StartBitcoind(ctx context.Context, configPath string, g *errgroup.Group) error {
logger := utils.ExtractLogger(ctx, "eunod")
cmd := exec.Command(
"/app/eunod",
fmt.Sprintf("--conf=%s", configPath),
)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
g.Go(func() error {
return logPipe(ctx, stdout, bitcoindLogger)
})
g.Go(func() error {
return logPipe(ctx, stderr, bitcoindStdErrLogger)
})
if err := cmd.Start(); err != nil {
return fmt.Errorf("%w: unable to start eunod", err)
}
g.Go(func() error {
<-ctx.Done()
logger.Warnw("sending interrupt to eunod")
return cmd.Process.Signal(os.Interrupt)
})
return cmd.Wait()
}

从现在起,我将链接到github。

在线:https://github.com/ScArFaCe2020/rosetta-euno/blob/master/bitcoin/node.go#L68-L71是exec。命令

fmt。Sprintf("--conf=%s",configPath(,工作正常。现在我需要在里面添加--daemon。

但当我把它改成时

fmt.Sprintf("--daemon --conf=%s", configPath),

它不起作用。

有人知道我如何添加--daemon吗?

exec.Command接受可变字符串参数。它的签名看起来是这样的。

func Command(name string, arg ...string) *Cmd

要么传递一段字符串,后跟...,要么传递多个字符串作为参数。

在您的案例中,您将整个字符串--daemon --conf=/some/path视为一个单独的参数,而实际上您希望将其视为两个不同的参数。

解决这个问题最简单的方法就是这样做。

cmd := exec.Command(
"/app/eunod",
"--daemon",
fmt.Sprintf("--conf=%s", configPath),
)

你可以在这里了解更多关于变异论点的信息https://golangdocs.com/variadic-functions-in-golang例如

最新更新