doufei4418 2018-05-03 03:38
浏览 301
已采纳

在Windows上编译要在Linux上部署的GO程序

I would like to develop on a windows environment and have installed go 1.10.1... I need to deploy my program on a linux however. Is there a way to build the go program and specify to build for linux. I have seen tons of documentation for going from linux to windows but not the other way around.

The most promising example I have tried:

set GOOS=linux 
set GOARCH=amd64 
go build filename.go

--> which just builds an exe..

  • 写回答

1条回答 默认 最新

  • douwen1929 2018-05-03 06:59
    关注

    I can confirm that what you are trying to achieve works for me on Windows 7 and Windows 10 with go 1.10.2. To check that your go install supports your desired target, run this command:

    go tool dist list | find "linux/amd64"
    

    You should make sure that you set the environment variables without any additional or hidden spaces, set them this way:

    set GOOS=linux
    set GOARCH=amd64
    

    Then verify, for example this way:

    echo !%GOOS%!
    echo !%GOARCH%!
    

    If the combination is not valid, you should get this message:

    cmd/go: unsupported GOOS/GOARCH pair linux /amd64

    After this run your build:

    go build -v -x -o foreign_linux foreign.go
    

    Following Volkers advice you can check if the created binary really is a linux binary:

    type foreign_linux
    

    You will receive some cryptic characters, but at the beginning of the first line should be ELF. If it is an exe, you will see MZ.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度