想要通过使用Go程序运行具有标志和参数的二进制文件

I want to run a binary file with flags. If i directly run the binary it will be like following inside a golang program.

./test --flag1 arg1 --flag2 arg2

I was trying to run by the use of os.exec.

code: reslt ,err:= exec.Command("./test","--flag1", "arg1", "--flag2", "arg2").Output

It is giving error:

Exit status 2

Can anyone help on this?

output, err := exec.Command("./test","flag1", "arg1", "flag2", "arg2").Output()

Output returns both a slice of bytes and an error. As indicated by the error, you are only expecting a single return value, whereas Output returns two.

EDIT: As for debugging your second problem, get the stderr from the command:

cmd := exec.Command("./test","flag1", "arg1", "flag2", "arg2")
var stderr bytes.Buffer
cmd.Stderr = &stderr  
err := cmd.Run()
if err != nil {
    fmt.Println(stderr.String())
    return
}

I guess Exit code 2 means 'no such a file or directory'. You should check path where you executed go binary and where test is.

Please try to specify absolute path for your cimmand first.