Thursday 27 November 2014

go installation and build with cross platform compilation

On this note:

1. We do the installation of go on the mac
2. Then create a simple hello world program
3. Execute and test
4. Build the code [ for this example its for mac]
5. Setup cross platform build



Installation of go [ for this example on mac ]

  • For detail information: link: http://golang.org/doc/install
  • installed from the tar.gz, but you can try from mac pkg file too.
  • created a directory "go" under "/usr/local"
  • downloaded go1.3.3.darwin-amd64-osx10.8.tar.gz from https://golang.org/dl/
  • copied the file to /usr/local folder.
  • change the ownership or give the full access to your current user for /usr/local/go folder
  • extract the your download file go1.3.3.darwin-amd64-osx10.8.tar.gz it will extract everything to your /usr/local/go folder
  • update your PATH with adding /usr/local/go/bin
  • type go and you should see the go usage output. [ it mean you have successfully install go :) ]

Create a simple hello world program


package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}

 Execute and test

$ go run hello.go

hello, world


Build the code [ for this example its for mac]

$go build -o hello-mac hello.go

The above command will create an mac executable file.

$file hello-mac
hello-mac: Mach-O 64-bit executable x86_64

now you can run the code directly to the mac os:
$ ./hello-mac
hello, world


Setting up cross build

Further detail information: Link:
http://dave.cheney.net/2013/07/09/an-introduction-to-cross-compilation-with-go-1-1

$ cd /usr/local/go/scr
./all.bash [ if you get some error, try with sudo , just incase if you don't have write access. ]


https://github.com/davecheney/golang-crosscompile

$ git clone git://github.com/davecheney/golang-crosscompile.git
$ source golang-crosscompile/crosscompile.bash
[ if you get some error, try with sudo , just incase if you don't have write access. ]

go-crosscompile-build-all
[ if you get some error, try with sudo , just incase if you don't have write access. ]



This will compile the Go runtime and standard library for each platform. You can see these packages if you look in go/pkg.

% ls -1 go/pkg
darwin_386
darwin_amd64
freebsd_386
freebsd_amd64
linux_386
linux_amd64
linux_arm
obj
tool
windows_386
windows_amd64

And in your shell you can find all the following commands:

go                         go-darwin-386              go-linux-386               go-windows-386
go-all                     go-darwin-amd64            go-linux-amd64             go-windows-amd64
go-build-all               go-freebsd-386             go-linux-arm               godoc
go-crosscompile-build      go-freebsd-amd64           go-openbsd-386             gofmt
go-crosscompile-build-all  go-freebsd-arm             go-openbsd-amd64



go-build-all hello.go [ This will build your code for all the platform :) ]

and you can see all your build file:
hello-darwin-386 hello-freebsd-arm hello-mac hello-windows-amd64
hello-darwin-amd64 hello-linux-386 hello-openbsd-386 hello.go
hello-freebsd-386 hello-linux-amd64 hello-openbsd-amd64
hello-freebsd-amd64 hello-linux-arm hello-windows-386

I have tested and found, I can able to run the code in different arch.
NOTE: for windows build file, rename them to .exe before running them.


hello-darwin-386:    Mach-O executable i386
hello-darwin-amd64:  Mach-O 64-bit executable x86_64
hello-freebsd-386:   ELF 32-bit LSB executable, Intel 80386, version 1 (FreeBSD), statically linked, not stripped
hello-freebsd-amd64: ELF 64-bit LSB executable, x86-64, version 1 (FreeBSD), statically linked, not stripped
hello-freebsd-arm:   ELF 32-bit LSB executable, ARM, version 1 (FreeBSD), statically linked, not stripped
hello-linux-386:     ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
hello-linux-amd64:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped
hello-linux-arm:     ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
hello-openbsd-386:   ELF 32-bit LSB executable, Intel 80386, version 1 (OpenBSD), statically linked, for OpenBSD, not stripped
hello-openbsd-amd64: ELF 64-bit LSB executable, x86-64, version 1 (OpenBSD), statically linked, for OpenBSD, not stripped
hello-windows-386:   PE32 executable for MS Windows (console) Intel 80386 32-bit
hello-windows-amd64: PE32+ executable for MS Windows (console) Mono/.Net assembly
hello.go:            ASCII Java program text  <---- For me at mac it shows like this :)


No comments:

Post a Comment