package main
import (
"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:\go\file.go`)
}