通过阅读这篇文章您应当可以为任何系统构建 Go 语言。

先决条件

1. 从 Go 1.5 开始,Go 语言所有的源代码都使用了 Go 或者汇编语言。因此在一个安装有 Go 语言的系统中使用 Go 语言构建另一个 Go 语言版本会十分简单。这个特性叫做语言的自举

GOOSGOARCH

因此,为我的 Lenovo Newifi 2(D2) 编译 Go 语言环境是可行的。这个路由器的官方固件基于 Openwrt,处理器为 MT7621AT,MIPS 架构小端序,配有 256M 内存。

我希望能保留官方固件而非刷机,因此我决定为我的路由器构建一个独立的 Go 语言版本。

准备

在您的系统上安装 Go>=1.5 版本:


> which go

/usr/bin/go

> go version

go version go1.10.4 linux/amd64

如果您没有安装 Go 语言,请使用您发行版的包管理器安装。对于 Ubuntu 来说


> sudo apt-get install golang-go

从 下载页面 获取 Go 语言源代码。

我使用了 Golang 1.12.4:


> wget https://dl.google.com/go/go1.12.4.src.tar.gz

构建

解压代码包:


> tar xvf go1.12.4.src.tar.gz

目录结构如下:


.

├── api

├── AUTHORS

├── CONTRIBUTING.md

├── CONTRIBUTORS

├── doc

├── favicon.ico

├── lib

├── LICENSE

├── misc

├── PATENTS

├── README.md

├── robots.txt

├── src

├── test

└── VERSION

src

> GOOS=linux GOARCH=mipsle GOROOT_BOOTSTRAP=<your-go-root> ./make.bash

Building Go cmd/dist using <your-go-path>/go-1.10.

Building Go toolchain1 using <your-go-path>/go-1.10.

Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.

Building Go toolchain2 using go_bootstrap and Go toolchain1.

Building Go toolchain3 using go_bootstrap and Go toolchain2.

Building packages and commands for host, linux/amd64.

Building packages and commands for target, linux/mipsle.

---

Installed Go for linux/mipsle in <your-build-path>/go

Installed commands in <your-build-path>/go/bin

安装

构建完成后,将以下文件夹复制到路由器:


api 

bin 

misc 

pkg 

src 

test

/mnt/mmcblk0p1/usr/share/go
bin/mipslegogofmtbin
GOROOTbin

> export GOROOT=/mnt/mmcblk0p1/usr/share/go

> PATH=$GOROOT/bin:$PATH

运行您的第一个 Go 程序
test.go

package main

import (

        "fmt"

)

func main() {

      fmt.Println("hello newifi2")

}

直接运行它:


> go run test.go

hello newifi2

或者构建+运行:


> go build test.go

> ./test

hello newifi2

一切 OK!