這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
//調用exe
func callEXE1() {arg := []string{}cmd := exec.Command("test", arg...)//會向 cmd.Stdout和cmd.Stderr寫入資訊,其實cmd.Stdout==cmd.Stderr,具體可見源碼output, err := cmd.CombinedOutput()if err != nil {fmt.Println("Error:", err)return}fmt.Printf("callEXE2結果:\n%v\n\n%v\n\n%v", string(output), cmd.Stdout, cmd.Stderr)}func callEXE2() {arg := []string{}cmd := exec.Command("test", arg...)//會向 cmd.Stdout寫入資訊output, err := cmd.Output()if err != nil {fmt.Printf("Error: %s\n", err)return}fmt.Printf("callEXE2結果:\n%v\n%v\n%v", string(output), cmd.Stdout, cmd.Stderr)}
test.exe源碼:
//vs 2010 c++#include "stdafx.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){std::cout<<"中國,zhongguo2014"<<endl;//std::cerr<<"中國,zhongguo2014"<<endl;//std::clog<<"中國,zhongguo2014"<<endl;fprintf(stderr,"中國,zhongguo2013");//fprintf(stdout,"中國,zhongguo2013");return 0;}