Go 从fmt.Printf更改为exec.Command

Go 从fmt.Printf更改为exec.Command,go,Go,如何更改代码而不是显示代码(fmt.Printf)以执行命令(exec.command) 现在我有这个: //打印密钥 fmt.Printf(“%x%34s%34s\n”,填充,uaddr.EncodeAddress(),caddr.EncodeAddress()) 如何为“g”和“h”指定变量值: v := "cmd" n := "/C" a := "testcmd" b := "-connect=127.0.0.1" c := "-port=3333"

如何更改代码而不是显示代码(fmt.Printf)以执行命令(exec.command)

现在我有这个:

//打印密钥
fmt.Printf(“%x%34s%34s\n”,填充,uaddr.EncodeAddress(),caddr.EncodeAddress())

如何为“g”和“h”指定变量值:

    v := "cmd"
    n := "/C"
    a := "testcmd"
    b := "-connect=127.0.0.1"
    c := "-port=3333"
    d := "-user=username"
    e := "-password=password"
    f := "importaddress"
    g := "AddressHere"
    h := "MoreInfo"

    z := exec.Command(v, n, a, b, c, d, e, f, g, h)

    if err := z.Run(); err != nil { 
        fmt.Println("Error: ", err)
    }
我需要给这个变量赋值:

h := fmt.Printf("%x\n", padded)
g := fmt.Printf("%34s\n", uaddr.EncodeAddress())
g := fmt.Printf("%34s\n", caddr.EncodeAddress()) 

使用不同的变量执行命令两次

您可以使用
fmt.Sprintf()

例如:

g := fmt.Sprintf("%s", uaddr.EncodeAddress())

Sprintf根据格式说明符格式化并返回结果字符串。然后可以对变量使用相同的值。

使用fmt.Sprintf。