Godson's way to go - install go in Godson

Tools and Resource Center

Help developers work more efficiently and provide tools and resources around the whole life cycle of developers
developer.aliyun.com/tool?spm=a1z3...

Go language configuration of generation 2 of Godson school and installation of go opencv

  • Go language is a rising star. It is very necessary for us to configure the environment required for go language on the domestic hardware of Godson school. At the same time, we also install opencv under go language

1, Directly use go in the yum source installation package to install

  • You can use yum install directly. You can use yum search to find it

2, Build Go locale from source code

As the stable version of the latest released Go language is Go 1.6, and the official supports MIPS architecture processors from this version, the author takes Go 1.6 as an example to show the construction method of Go language environment on Godson platform. In order to minimize the dependence of Go language on other programming tools, starting from Go 1.5, Go language compiler, assembler, linker and other low-level tool chains are all implemented in Go language. Therefore, since Go 1.5, when installing the Go language environment from the source code, it is necessary to use the ready-made Go language compiler to compile the source code of Go. The above process is called the bootstrap compilation of Go language. Therefore, when installing Go 1.6 from source code, it is required that there is a Go locale of Go 1.4 and above in the system. Since the official MIPS architecture has only been officially supported since Go 1.6, it is impossible to find a ready-made low version Go compiler that can be used for bootstrap compilation on Godson. Fortunately, the Go language provides a friendly cross compilation function. We can compile a Go compiler on MIPS platform with the help of X86 machine, which can be used as a bootstrap compiler to build Go 1.6 from the source code on Godson. Therefore, the environment for building Go 1.6 from source code on Godson platform must be divided into two stages:

1) Cross compile the Go language bootstrap compilation tool chain for MIPS platform on X86 platform;

2) Use the Go bootstrap compiler in 1) to build the Go 1.6 development environment on Godson platform.

Next, the author will gradually show you the construction process of Go 1.6 environment on Godson platform. When you Go through the whole process, you will find that it is very simple to install the Go language environment from the source code on Godson.

Phase I: cross compile the Go language bootstrap compilation tool chain for MIPS platform on X86 platform.

Find any X86 computer with 64 bit Linux operating system and gradually execute the following commands.

1. Download the binary distribution of Go 1.4 (or higher) as the bootstrap compilation tool chain for cross compilation. The author selects the distribution of Go 1.4 as the bootstrap compiler for compiling Go 1.6 source code on X86. Due to the official website https://golang.org If it cannot be accessed normally in China, it can be accessed through its mirror site http://golangtc.com/download Download. The file downloaded by the author is go1.4.2.linux-amd64.tar.gz, with a size of about 59 M. After downloading, you can unzip it to any directory. For example, you can unzip it to the / opt folder and rename it go1.4.2 with the following command:

$ tar xvzf go1.4.2.linux-amd64.tar.gz -C /opt
$ mv /opt/go /opt/go1.4.2

2. Configure the bootstrap compilation tool chain for cross compilation

$ export GOROOT_BOOTSTRAP=/opt/go1.4.2

3. Download the source code of Go 1.6

$ cd ~/workspace/golang/
$ git clone https://github.com/golang/go.git

4. Switch to the release of Go 1.6

$ cd go
$ git checkout release-branch.go1.6

5. Cross compiling is completed by executing the script bootstrp.bash. Before executing the script, you need to specify the target operating system and processor architecture of cross compilation through the environment variables GOOS and GOARCH respectively.

$ cd ~/workspace/golang/go/src
$ GOOS=linux GOARCH=mips64le ./bootstrap.bash

The next step is to wait for the cross compilation to end. Because cross compilation does not need to execute the official test set of Go language, it can be completed within 10 minutes. If nothing happens, the whole process should be very smooth. If you see the following prompt, the cross compilation is successful.

----
Bootstrap toolchain for linux/mips64le installed in /home/fool/workspace/golang/go-linux-mips64le-bootstrap.
Building tbz.
-rw-rw-r-- 1 fool fool 46736199  6 March 09:08 /home/fool/workspace/golang/go-linux-mips64le-bootstrap.tbz

The Go language bootstrap compiler chain for MIPS platform generated by cross compilation is located in the compressed package / home / pool / workspace / golang / Go linux-mips64le-bootstrap.tbz. Copy the compressed package to Godson computer for standby.

Phase II: build Go 1.6 development environment from source code on Godson platform.

1. Decompress the bootstrap compilation tool chain for MIPS platform generated by cross compilation

$ tar xvjf go-linux-mips64le-bootstrap.tbz -C ~/workspace

2. Simply check whether the bootstrap compilation tool chain is available

$ cd ~/workspace/go-linux-mips64le-bootstrap/bin
$ export GOROOT=/home/loongson/workspace/go-linux-mips64le-bootstrap
$ ./go version

If the following output is observed, it basically shows that cross compilation is no problem.

go version go1.6.2 linux/mips64le

3. Download the source code of Go 1.6 and switch to the distribution of Go 1.6.

$ cd ~/workspace
$ git clone https://github.com/golang/go.git
$ cd go
$ git checkout release-branch.go1.6

4. Compile the configuration, create a shell script named env.sh in the current directory, enter the following statement, save and exit.

#!/bin/bash
export GOROOT_BOOTSTRAP=/home/loongson/workspace/go-linux-mips64le-bootstrap
export GOROOT=/home/loongson/workspace/go

Among them, GOROOT_BOOTSTRAP is used to point to the root directory of bootstrap compilation tool chain, and GOROOT points to the root directory of Go 1.6 source code. Execute env.sh to complete the compilation configuration.

$ source env.sh

5. First compilation

$ cd src
$ ./all.bash

When the above script is executed, the bootstrap compilation tool chain will be used to compile the Go 1.6 source code and generate the Go 1.6 environment on Godson. The generated local Go 1.6 is then tested using the official test set. The test process is relatively long and requires patience. After about 20 minutes, the following error message is suddenly printed during the test:

ok      cmd/compile/internal/test    0.038s
ok      cmd/cover    18.360s
ok      cmd/doc    0.393s
ok      cmd/fix    0.192s
panic: test timed out after 3m0s

goroutine 489 [running]:
panic(0x120552480, 0xc4203087f0)
    /home/loongson/workspace/go/src/runtime/panic.go:500 +0x4c4
testing.startAlarm.func1()
    /home/loongson/workspace/go/src/testing/testing.go:918 +0x168
created by time.goFunc
    /home/loongson/workspace/go/src/time/sleep.go:132 +0x5c
...
FAIL    cmd/go    208.996s
ok      cmd/gofmt    0.270s
ok      cmd/internal/goobj    0.064s
ok      cmd/internal/obj    0.032s
ok      cmd/internal/obj/x86    0.067s
ok      cmd/internal/pprof/profile    0.048s
ok      cmd/link    0.077s
ok      cmd/nm    9.304s

The test item cmd/go failed the test. Why? At first, the author suspected that there might be a problem with the compiled Go 1.6, and repeatedly checked the setting of compilation parameters, but the error remained. Later, considering that the official has announced that Go 1.6 directly supports MIPS, the probability of compilation errors should be very small, not to mention that so many previous test projects can run correctly. Therefore, the idea is changed from checking the compilation parameters to checking the test parameters. Obviously, the error is caused by the test timeout, and the default timeout threshold is only 3 minutes. For low frequency processors such as Godson and ARM, the execution time of cmd/go test item may exceed 3 minutes. Therefore, it may be necessary to appropriately increase the threshold of test timeout on Godson platform. Later, after a series of analysis and troubleshooting, the environmental variable go was finally found_ TEST_ TIMEOUT_ Scale, used to multiply the test timeout threshold. For example, if go is set_ TEST_ TIMEOUT_ If scale = 2, the test timeout threshold is doubled, that is, from 3 minutes to 6 minutes.

6. Recompile. Add the environment variable go in env.sh according to the needs of the specific platform_ TEST_ TIMEOUT_ Scale settings, for example, the following is env.sh applicable to Godson platform.

#!/bin/bash

export GOROOT_BOOTSTRAP=/home/loongson/workspace/go-linux-mips64le-bootstrap
export GOROOT=/home/loongson/workspace/go

# Added for Loongson
export GO_TEST_TIMEOUT_SCALE=2

After the above modifications are completed, re execute the env.sh and all.bash scripts.

$ cd ~/workspace/go
$ source env.sh
$ cd src
$ ./all.bash

After the script is executed, if the following output information is observed, it indicates that the compilation and testing of Go 1.6 on Godson platform have been successfully completed. The whole process takes about half an hour.

ALL TESTS PASSED
---
Installed Go for linux/mips64le in /home/loongson/workspace/go
Installed commands in /home/loongson/workspace/go/bin
*** You need to add /home/loongson/workspace/go/bin to your PATH.

7. Install Go 1.6 generated by compilation. Add the following statement at the end of ~ /. bashrc

export GOROOT=/home/loongson/workspace/go
export PATH=$PATH: $GOROOT/bin

Then execute

$ source ~/.bashrc

3, Greetings from Go language to the world on Godson

The exciting moment has finally arrived! Let's run the Go command first and print the version information:

$ go version
go version go1.6.2 linux/mips64le

Next, let's test Go's "Hello World"! Open any text editor, you can use the classic vim of Linux or gedit, which is more suitable for the public, to write the hello.go file in just a few lines of code:

package main
import "fmt"
func main() {
    fmt.Printf("Hello World!\n")
}

First compile the source code hello.go. Go, like C/C + + language, is a statically compiled language. Go's source program must be compiled before it can be executed.

$ go build hello.go

The above command generates an executable named hello. Run hello

$ ./hello
Hello World!

It's really exciting to personally feel the first HelloWorld of Go language on Godson! The above compilation and execution process can also be completed at one time by using the go run command, for example

$ go run hello.go
Hello World!

This article is transferred from: Godson's road to go (I) - install go in Godson - Alibaba cloud developer community

Keywords: Go

Added by vinoth on Sun, 21 Nov 2021 07:56:52 +0200