go语言在Windows系统下编译成linux系统可执行文件

时间:2019-10-07 22:49:37   收藏:0   阅读:674
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`)
}

原文:https://www.cnblogs.com/traditional/p/11632496.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!