1. 程式人生 > >golang執行命令 exec.Command

golang執行命令 exec.Command

	cmd := exec.Command("cmd")
	in := bytes.NewBuffer(nil)
	cmd.Stdin = in//繫結輸入
	var out bytes.Buffer
	cmd.Stdout = &out //繫結輸出
	go func() {
		in.WriteString("node E:/design/test.js\n")//寫入你的命令,可以有多行,"\n"表示回車
	}()
	err = cmd.Start()
	if err != nil {
		log.Fatal(err)
	}
	log.Println(cmd.Args)
	err = cmd.Wait()
	if err != nil {
		log.Printf("Command finished with error: %v", err)
	}
	fmt.Println(out.String())