博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang 跨平台编译——go 在windows上编译Linux平台的程序(Cross Compilation from Windows to Linux/Ubuntu)...
阅读量:5914 次
发布时间:2019-06-19

本文共 1636 字,大约阅读时间需要 5 分钟。

Go Cross Compilation from Windows to Linux/Ubuntu

 I have GO 1.7 installed on my Windows 10. I created test program and it works perfectly in Windows. Next step is to try to run it on my docker virtual machine with Ubuntu.

 

Answer:

That other question is a bit old (from 2013).

Cross-platform support evolved a lot, all you need to do is change the environment variables (GOARCH and GOOS) and run go build.

Navigate to the folder of the main package, then:

set GOARCH=amd64set GOOS=linux go build

You may change the name of the result binary like this:

go build -o "myapp"

Note that in Linux to compile the app for Windows amd64, a single command is enough (in the folder of the main package):

GOOS=windows GOARCH=amd64 go build

This is also confirmed in blog post: :

To cross compile a Go program using Go 1.5 the process is as follows:

  1. set GOOS and GOARCH to be the values for the target .

  2. run go build -v YOURPACKAGE

Notes

You don't have to, and you shouldn't run go install, as that will compile and install the packages in your GOPATH, which is often not wanted. Doing cross compilation is not for developing / testing your app and packages. It is to produce a single binary which you will run on another system / computer.

go build will not install anything, it will just produce the executable binary. For details, see 

Also confirmed in blog post: :

When cross compiling, you should use go build, not go install. This is the one of the few cases where go build is preferable to go install.

The reason for this is go install always caches compiled packages, .a files, into the pkg/directory that matches the root of the source code.

转载地址:http://wwwvx.baihongyu.com/

你可能感兴趣的文章
Codeforces Round #219 (Div. 1) A. Counting Kangaroos is Fun 【二分】
查看>>
Html基础
查看>>
wiki----为用户设置管理员权限
查看>>
Codeforces Round #565 (Div. 3) A. Divide it!
查看>>
《图像处理实例》 之 局部极值提取
查看>>
java 常见几种发送http请求案例[转]
查看>>
更改Visual Studio 2010/2012/2008的主题设置
查看>>
win7系统安装hadoop
查看>>
day5作业购物商城+ATM
查看>>
day6作业--选课系统
查看>>
stegsolve---图片隐写查看器
查看>>
dubbo接口测试
查看>>
Maven生命周期详解(转)
查看>>
uoj#401. 【CTSC2018】青蕈领主(分治FFT)
查看>>
jvm -Xms -Xmx
查看>>
bash的pushd和popd
查看>>
2018 German Collegiate Programming Contest (GCPC 18)
查看>>
前端之jquery
查看>>
静态类和非静态类
查看>>
关于日志表的自动创建及分表储存
查看>>