1 Package Main2 3 Import (4 "OS"5 "os/exec"6 "FMT"7 "Flag"8 "Strings"9 )Ten One Func Main () { ACommand: = flag. String ("cmd","pwd","Set the command.") -Args: = flag. String ("args","","Set the args. (separated by spaces)") -Flag. Usage =func () { theFmt. fprintf (OS. Stderr,"Usage:%s [-cmd <command>] [-args <the arguments (separated by spaces) >]\n", OS. args[0]) - flag. Printdefaults () - } - flag. Parse () +Fmt. Println ("Command:", *command) -Fmt. Println ("Arguments:", *args) + varArgarray []string A if*args! ="" { atArgarray = strings. Split (*args," ") -}Else { -Argarray = make ([]string,0) - } -CMD: = Exec.command (*command, Argarray ...) -BUF, err: =cmd. Output () in ifErr! =Nil { -Fmt. fprintf (OS. Stderr,"the command failed to perform:%s (command:%s, Arguments:%s)", err, *command, *args) to return + } -Fmt. fprintf (OS. Stdout,"Result:%s", BUF) the}
From Http://outofmemory.cn/code-snippet/1125/Go-language-execution-xitongmingling-row-command
Go language Execution System command line command (GO)