doufei4418
doufei4418
2018-05-03 03:38

在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 douwen1929 3年前

    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

    点赞 评论 复制链接分享