Question

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

I found out this happens when I do not generate the binary file from the same OS. I do go build with Mac OS but I need to run this binary from Vagrant that uses Ubuntu Linux. What command should I use instead of go build from Mac so that I can run the binary program in Vagrant environment?

Was it helpful?

Solution 2

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

OTHER TIPS

Note: with the upcoming Go 1.5 (Q3 2015), this will be easier.

See "Cross compilation just got a whole lot better in Go 1.5"
(still from Dave Cheney)

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top