8
votes

I'm using windows 7 [32 bit] operating system.

I'm build example go program.

I want to compile this program for all platforms from my windows 7 [32 bit] OS.

I want to compile my program for all Linux [32/64] / Mac OSX [32/64] / Windows[32/64].

Is it possible are not from my single OS ?

2

2 Answers

11
votes

Update Go 1.5: see "Cross compilation just got a whole lot better in Go 1.5"

For successful cross compilation you would need

  • compilers for the target platform, if they differed from your host platform, ie you’re on darwin/amd64 (6g) and you want to compile for linux/arm (5g).
  • a standard library for the target platform, which included some files generated at the point your Go distribution was built.

With the plan to translate the Go compiler into Go coming to fruition in the 1.5 release the first issue is now resolved.

package main

import "fmt"
import "runtime"

func main() {
        fmt.Printf("Hello %s/%s\n", runtime.GOOS, runtime.GOARCH)
}

build for darwin/386

% env GOOS=darwin GOARCH=386 go build hello.go
# scp to darwin host
$ ./hello
Hello darwin/386

Or build for linux/arm

% env GOOS=linux GOARCH=arm GOARM=7 go build hello.go
# scp to linux host
$ ./hello
Hello linux/arm

Original answer (Go 1.4 and before)

You can try a tool like gox

Gox is a simple, no-frills tool for Go cross compilation that behaves a lot like standard go build.
Gox will parallelize builds for multiple platforms.
Gox will also build the cross-compilation toolchain for you.

Before you use Gox, you must build the cross-compilation toolchain. Gox can do this for you. This only has to be done once (or whenever you update Go):

$ gox -build-toolchain

You will also find many cross-platform development tips at "Developing for Multiple Platforms With Go".

Passionate Developer points out below to issue 19, left by the OP Nakka Chandra, even though issue 10 reported making gox run successfully on Windows.

1
votes

On Windows run the following commands:

C:\Users\user\go\src\myapp> set GOOS=linux  
C:\Users\user\go\src\myapp> set GOARCH=amd64  
C:\Users\user\go\src\myapp> go build  

It worked for me.

Notice, if you get the error:

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

This is because you have a space at the end of the variable.
Example, wrong use is: set GOOS=linux<space>), instead it should be: set GOOS=linux.

This is the full table list (taken from here) for all the other systems:

    GOOS - Target Operating System| GOARCH - Target Platform
   -------------------------------|--------------------------
   |           android            |           arm           |  
   |           darwin             |           386           |  
   |           darwin             |           amd64         |    
   |           darwin             |           arm           |  
   |           darwin             |           arm64         |    
   |           dragonfly          |           amd64         |    
   |           freebsd            |           386           |  
   |           freebsd            |           amd64         |    
   |           freebsd            |           arm           |  
   |           linux              |           386           |  
   |           linux              |           amd64         |    
   |           linux              |           arm           |  
   |           linux              |           arm64         |    
   |           linux              |           ppc64         |    
   |           linux              |           ppc64le       |      
   |           linux              |           mips          |   
   |           linux              |           mipsle        |     
   |           linux              |           mips64        |     
   |           linux              |           mips64le      |       
   |           netbsd             |           386           |  
   |           netbsd             |           amd64         |    
   |           netbsd             |           arm           |  
   |           openbsd            |           386           |  
   |           openbsd            |           amd64         |    
   |           openbsd            |           arm           |  
   |           plan9              |           386           |  
   |           plan9              |           amd64         |    
   |           solaris            |           amd64         |    
   |           windows            |           386           |   
   |           windows            |           amd64         |
   ----------------------------------------------------------