复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60// study project main.go package main import ( "fmt" "os" "os/exec" "path/filepath" ) type buildObj struct { gopath string //GOPATH appdir string //微服务main函数所在目录 goArch string //编译目标架构 appName string //生成的app名称 } func main() { buildInfo := []string{ `E:work-spacecaozw`, "caozw", "386", "caozw32.exe", } buildInfo2 := []string{ `E:work-spacecaozw`, "caozw", "amd64", "caozw64.exe", } buildList := []interface{}{buildInfo, buildInfo2} for _, oneBuild := range buildList { buildOneApp(oneBuild.([]string)) } } func buildOneApp(buildInfo []string) { buildObj := new(buildObj) buildObj.gopath = buildInfo[0] buildObj.appdir = buildInfo[1] buildObj.goArch = buildInfo[2] //"386" ,"amd64" buildObj.appName = buildInfo[3] ///切换到要编译的目标代码 os.Chdir(filepath.Join(buildObj.gopath, "src", buildObj.appdir)) pwd, _ := os.Getwd() fmt.Println(pwd) os.Setenv("GOPATH", buildObj.gopath) os.Setenv("GOARCH", buildObj.goArch) fmt.Println(os.Getenv("GOPATH")) fmt.Println(os.Getenv("GOARCH")) cmd := exec.Command("go", "build", "-o", buildObj.appName, "-i", "-ldflags", "-H windowsgui") err := cmd.Run() if nil != err { fmt.Println("编译app报错,app名称 错误信息:", buildObj.appName, err) } else { fmt.Println("编译app完成,app名称:", buildObj.appName) } }
复制代码
1
2
复制代码
1
2
复制代码
1
2
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Go是一门编译型语言,所以在不同平台上,需要编译生成不同格式的二进制包。
由于Go 1.5对跨平台编译有了一些改进,包括统一了编译器、链接器等。
编译时候只需要指定两个参数:GOOS
和GOARCH
即可。示例:
复制代码
# 编译到 linux 64bit $ GOOS=linux GOARCH=amd64 go build # 或者可以使用 -o 选项指定生成二进制文件名字 $ GOOS=linux GOARCH=amd64 go build -o app.linux # 编译到 linux 32bit $ GOOS=linux GOARCH=386 go build # 编译到 windows 64bit $ GOOS=windows GOARCH=amd64 go build # 编译到 windows 32bit $ GOOS=windows GOARCH=386 go build # 编译到 Mac OS X 64bit $ GOOS=darwin GOARCH=amd64 go build
复制代码
最后
以上就是自觉星星最近收集整理的关于go 自用编译程序代码的全部内容,更多相关go内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复