我知道如何在C中执行此操作,但如何在Go中执行此操作?
这是我使用的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMN_LEN 100
int main(int argc, char *argv[])
{
char cmd[MAX_CMN_LEN] = "", **p;
if (argc < 2) /*no command specified*/
{
fprintf(stderr, "Usage: ./program_name terminal_command ...");
exit(EXIT_FAILURE);
}
else
{
strcat(cmd, argv[1]);
for (p = &argv[2]; *p; p++)
{
strcat(cmd, " ");
strcat(cmd, *p);
}
system(cmd);
}
return 0;
}
答案 0 :(得分:1)
这样的事情:
package main
import (
"os"
"os/exec"
)
func main() {
if len(os.Args) < 2 {
panic("Usage: ./program_name terminal_command ...")
}
cmd := exec.Command(os.Args[1], os.Args[2:]...)
cmd.Run()
}