package mainimport ("fmt""os""os/exec"
)//filepath: 要編譯的文件的路徑
func build(filepath string){_ = os.Setenv("CGO_ENABLED", "0")_ = os.Setenv("GOARCH", "amd64")_ = os.Setenv("GOOS", "linux")arg := []string{"build", filepath}if err := exec.Command("go", arg...).Run(); err!=nil {fmt.Println("編譯失敗:", err)} else{fmt.Println("編譯成功")}
}func main() {build(`D:\gopathdir\src\httpproxy\main.go`) //被編譯的go文件
}
這是轉載的,但是地址忘了。。。。