Answers for "golang run command line"

Go
1

go execute command

// ExecuteCommand execute cmd and returns the output
func ExecuteCommand(cmd string, args []string) ([]byte, error)  {
	cmd :=  exec.Command(cmd, args)
    buf, err := cmd.CombinedOutput()
    if err != nil {
    	return nil, fmt.Errorf("cmd run failed: %v, msg: %s", err, string(buf))
    }
    return string(buf), nil 
}
Posted by: Guest on February-16-2022
0

how to execute linux command in golang

cmd := exec.Command("echo", "hello wordl")
res, _ := cmd.CombinedOutput()
fmt.Println(string(res))
Posted by: Guest on October-10-2021

Browse Popular Code Answers by Language