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

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

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?