This post will give you a guide to build Go on no matter which system.
Chinese Version
Prerequisite
- After Go 1.5, all the source codes are written in Go or Assembly. So in a system where there is a Go install, we can build another Go using Go itself. The feature is called bootstrapping.
- With
GOOS
andGOARCH
flag, we can build Go program for given platform with given architecture. This is cross compile of Go.
So, it’s possible to build a Go environment for my Lenovo Newifi 2 (D1), which is based on OpenWRT, equiped with MIPS Little Endian MT7621AT processor, 256MB memory.
But I want to keep the official firmware, so I decided to build an independant build of Go for my router.
Preparing
Get your Go>=1.5 installed in your system:
1 | > which go |
If you don’t have Go installed, please install it with your current package manager.
For Ubuntu,
1 | > sudo apt-get install golang-go |
Get Golang source code from the downloads page.
I chose to use Golang 1.12.4:
1 | > wget https://dl.google.com/go/go1.12.4.src.tar.gz |
Building
Decompress the tarball:
1 | > tar xvf go1.12.4.src.tar.gz |
We’ll have a directory like this:
1 | . |
Enter src
directory.
Begin directly your build for mipsle:
1 | > GOOS=linux GOARCH=mipsle GOROOT_BOOTSTRAP=<your-go-root> ./make.bash |
Installing
After the building, copy the following folders to the router:
1 | api |
For example, I created /mnt/mmcblk0p1/usr/share/go
folder for them.
Then move go
and gofmt
from bin/mipsle
to bin
.
Add GOROOT
to environment variables and bin
to PATH:
1 | > export GOROOT=/mnt/mmcblk0p1/usr/share/go |
Run your first Go program
Create the Hello World code in test.go
:
1 | package main |
Run it:
1 | > go run test.go |
or build and run it:
1 | > go build test.go |
Enjoy your journal with Go on your router!