How to cross compile Go program on Mac for Ubuntu?

vagrant@precise64:/vagrant$ sudo ./myprogram 
./myprogram: 1: ./myprogram: Syntax error: "(" unexpected

I found out that this happens when I do not create a binary file from the same OS. I do go buildwith Mac OS, but I need to run this binary from Vagrant, which uses Ubuntu Linux. Which command should I use instead of go buildwith a Mac so that I can run a binary program in a Vagrant environment?

+2
source share
2 answers

You need to set up a cross compilation environment (by creating a go compiler). Dave Cheney's blog has good instructions: http://dave.cheney.net/2013/07/09/an-introduction-to-cross-compilation-with-go-1-1

+1
source

: Go 1.5 (Q3 2015) .

. " - Go 1.5

( )

-

  • , , youre darwin/amd64 (6g), linux/arm (5g).
  • , , , Go.

Go Go, 1.5, .

package main

import "fmt"
import "runtime"

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

darwin/386

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

linux/arm

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

Source: https://habr.com/ru/post/1607218/


All Articles