Godson go! Guide to building the Go language environment on godson platform

Source: Internet
Author: User
Tags install go locale git clone

Godson Software Ecology Series--Godson go! Guide to building the Go language environment on godson platform
2016-07-05 godson Zhong Ke
1
First knowledge of Go language
The go language is an open-source programming language launched by Google in 2009 and is a system programming language crafted by the world's top computing scientists, such as Robert Griesemer, Kenthompson and Rob Pike. The most notable features of the go language are fast coding, efficient concurrency, and automatic memory management. In addition, the go language is optimized for programming in areas such as Web servers, storage systems, and databases, and simplifies the installation and deployment of application systems. Therefore, the go language by the vast number of programmers favor, more and more began to apply to Google and Baidu and other well-known Chinese and foreign enterprises products.

Go Language Image logo
For such a development in the cloud computing and Big Data era of the most promising programming language, godson platform can be well supported? Today we start from the source code of Go, and gradually build the go language environment on the godson platform. Although the author of the experimental machine for the installation of the godson open-source community version of the operating system Loongnix godson 3B Mini Computer, this method applies to all 64-bit operating system MIPS processor platform.
2
Download the source code for the Go language

To attract more community development power, Google has open source code for the Go language and is hosted on the world-famous GitHub site. We can get full source code for go language by executing the following command:
$ cd ~
$ git clone https://github.com/golang/go.git
The above command requires that you have a GIT version control tool installed on your computer. If you don't have git preinstalled on your PC, you can quickly install it with the following command:
$ sudo yum install git
3
Build the go locale from source code

Since the currently released stable version of the go language is go 1.6, and the official support of the MIPS architecture from this version of the processor, so I take go 1.6 as an example to show the go language environment on the Godson platform construction method. To minimize the reliance of the go language on other programming tools, from Go 1.5, the underlying toolchain, such as the Go language compiler, assembler, and linker, are all implemented using the Go language. So since go 1.5, installing the go locale from the source code requires using the ready-made go language compiler to compile the go source code, which is called the Go Language Bootstrap compilation. Therefore, when you install go 1.6 from the source code, you are required to have go 1.4 and above in the go language environment in the system.
Since the official support of the MIPS architecture from Go1.6, it is impossible to find a ready-made version of the go compiler that can be used for bootstrap compilation on godson. Fortunately, the go language provides a friendly cross-compiling function, we can use the X86 machine to compile a MIPS platform of the Go compiler, used as a godson on the build go 1.6 from the source of the Bootstrap compiler. Therefore, in the Godson platform from the source code to build the Go 1.6 environment must be divided into two stages:
1) Cross-compiling on the X86 platform to the MIPS platform of the Go Language bootstrap compiler tool chain;
2) Build the Go 1.6 development environment on the godson platform using the Go bootstrap compiler in 1.
The following author gradually shows you the process of building the Go 1.6 environment on the godson platform. When you go through the process, you will find that it is very simple to install the go locale from the source code on the godson.
The first stage: cross-compiling on the X86 platform forward to the MIPS platform of the Go Language bootstrap compiler tool chain. Find a X86 computer with a 64-bit Linux operating system and step through the following commands.
1. Download the binary release version of Go1.4 (or later) as a cross-compiled bootstrap build toolchain. The author chooses the release version of Go1.4 as the bootstrap compiler for compiling Go1.6 source code on X86. Because the official website https://golang.org is unable to visit in the country normally, can download through its mirror site http://golangtc.com/download. I downloaded the file for go1.4.2.linux-amd64.tar.gz, about the size of the M. After downloading, you can extract to any directory, for example, you can extract it to the/opt folder and rename it to go1.4.2 by the following command:
$ tar xvzf go1.4.2.linux-amd64.tar.gz-c/opt
$ mv/opt/go/opt/go1.4.2
2. Configuring a cross-compiled bootstrap build toolchain
$ Export goroot_bootstrap=/opt/go1.4.2
3. Download the Go1.6 source code
$ CD ~/workspace/golang/
$ git clone https://github.com/golang/go.git
4. Switch to the release version of Go1.6
$ CD Go
$ git checkout release-branch.go1.6
5. Cross-compiling
This step is done by executing a script bootstrp.bash. Before the script executes, it is necessary to indicate the target operating system and processor architecture for cross-compiling by 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 finish. Since cross-compilation does not require the official Go language test set, it is generally possible to compile in 10 minutes. No accident, the whole process should be very smooth. If you see the following prompt, 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 June 3 09:08/home/fool/workspace/golang/go-linux-mips64le-bootstrap.tbz
The Go language bootstrap compiler tool chain for the MIPS platform generated by cross-compilation is located in the/home/fool/workspace/golang/go-linux-mips64le-bootstrap.tbz of the compressed package. Copy the package to the godson computer for backup.
The second stage: Build the Go 1.6 development environment from the source code on the Godson platform.
1. Unzip the cross-compilation generated by the MIPS platform for the Bootstrap compiler toolchain
$ tar xvjf go-linux-mips64le-bootstrap.tbz-c ~/workspace
2. Simply verify that the bootstrap build tool chain is available
$ CD ~/workspace/go-linux-mips64le-bootstrap/bin
$ Export Goroot=/home/loongson/workspace/go-linux-mips64le-bootstrap
$./go version
If you observe the following output, it is essential to demonstrate that cross-compilation is not a problem.
Go version go1.6.2 Linux/mips64le
3. Download the Go1.6 source code and switch to the release version of Go 1.6.
$ CD ~/workspace
$ git clone https://github.com/golang/go.git
$ CD Go
$ git checkout release-branch.go1.6
4. Compiling the configuration
Create a shell script named env.sh in the current directory and save exit after entering the following statement.
#!/bin/bash
Export Goroot_bootstrap=/home/loongson/workspace/go-linux-mips64le-bootstrap
Export Goroot=/home/loongson/workspace/go
Where Goroot_bootstrap is used to point to the root of the bootstrap compilation toolchain, Goroot points to the root directory of the Go 1.6 source code.
Execute the env.sh to complete the compilation configuration.
$ source Env.sh
5. First-time compilation
$ cd src
$./all.bash
When the above script executes, the bootstrap compiler tool chain is used to compile the Go 1.6 source code and build the Go 1.6 environment on the godson. The resulting local go 1.6 is then tested using the official test set. The process of testing is relatively lengthy and needs to be patiently awaited. 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
Test Project Cmd/go did not pass the test. What is this for? The author initially suspected that the compilation generated go 1.6 may have a problem, repeatedly check the settings of the compilation parameters, but the error is still. Later, considering that the official has announced that go 1.6 directly support MIPS, the probability of compiling errors should be very small, not to mention that so many of the previous Test projects are able to run correctly. Then the thinking from the compilation parameters to check the test parameters to troubleshoot. Obviously, the error is caused by a test timeout, and the default timeout threshold is only 3 minutes. For low-frequency processors such as godson and arm, CMD/GO test items can take longer than 3 minutes to execute. Therefore, it may be necessary to increase the threshold of the test timeout appropriately on the godson platform. Later, after a series of analysis and troubleshooting, we finally found the environment variable Go_test_timeout_scale, which is used to multiply the threshold value of the test timeout. For example, if you set Go_test_timeout_scale = 2, the test time-out threshold expands to twice times the original, which increases from 3 minutes to 6 minutes.
6. Recompile
According to the needs of the specific platform in env.sh to increase the environment variable Go_test_timeout_scale settings, such as the following for the Godson platform env.sh.
#!/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 complete, re-execute both scripts env.sh and All.bash.

$ CD ~/workspace/go
$ source Env.sh
$ cd src
$./all.bash
If the following output information is observed after the execution of the script, the compilation and testing of Go 1.6 on the godson platform is completed successfully. The whole process is 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 compile-generated go 1.6.
Add the following statement at the end of the ~/.BASHRC
Export Goroot=/home/loongson/workspace/go
Export path= $PATH: $GOROOT/bin
And then execute
$ source ~/.BASHRC
4
Godson on Go language greetings to the world

The exciting moment has finally arrived! Let's run the Go command first and print out 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 Linux classic Vim, you can also use more suitable for the public gedit, write Hello.go file, 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 is a statically compiled language, just like the C + + language. The go source program must be compiled before it can be executed.
$ go Build hello.go
The above command generates an executable file named Hello. Run Hello
$./hello
Hello world!
Personally feel the go language in godson on the first HelloWorld, it is very exciting!
The process of compiling and executing the above can also be done using the Go Run command at once, for example
$ go Run hello.go
Hello world!
5
Keep playing with go!. Go! Go!

See here, some readers can not help asking: In addition to running simple HelloWorld, the godson platform on the go can also do? The go language has a convenient portability feature. All applications written in the go language can be run on the platform after it has been compiled by the appropriate platform's go language compiler. So the successful construction of the Go language environment on godson platform means that godson has a full set of software ecology of Go. The Go language based application system, previously built on platforms such as X86 or arm, is now able to move quickly to the godson platform at a lower cost.
There are so many open source projects based on go that I don't have time to build one by one. The author has fun, so specially selected a few more interesting items for the experiment. Interested friends can try more items. It is important to note that when testing open source projects, additional settings for Gopath environment variables are required. This is because many open source projects reference the third-party library of the Go language and need to load a third-party library from the path pointed to by the GOPATH environment variable or download the referenced third-party library to the path. For example, the author's settings are as follows:
$ Export Gopath=/home/loongson/workspace/3rdpkg
In order to facilitate the development of the Go Language program later, it is recommended to add the above environment variables to the ~/.BASHRC file.
1. Crash Bounce Game (Https://github.com/kurehajime/pong-command)
In the game, enter any string (such as go1.6.2), the string will move along the line, encountered the upper and lower boundary or left and right fence mirror bounce. The left fence is automatically controlled by the computer and the right fence is operated by the player. The goal of the game is to control the right side of the fence in the vertical position, so that the string is constantly bouncing back.

The impact bounce game on godson platform
2. Hanyu Pinyin Conversion tool (Https://github.com/mozillazg/go-pinyin)
Convert any Chinese character to the corresponding Hanyu Pinyin. For example, enter "Godson Club" to perform as follows:

Hanyu Pinyin Conversion tool on the Godson platform
3. Translation Assistant Utran (Https://github.com/zengsai/utran)
English can be translated into Chinese. For example, enter "This was go1.6.2 running on Loongson." Of

For example, enter "This was go1.6.2 running on Loongson." is implemented as follows:

Translation Assistant Utran on the godson platform
4. Sorting algorithm visualization (https://github.com/SimonWaldherr/GolangSortingVisualization) visualizes the sorting process of common bubble, select, insert, and fast sorting algorithms.

Visualization of sorting algorithms on godson platform
6
What to do if you encounter problems?
The above is the author on the Godson platform from the source code to build the Go language environment records. Following these steps, readers should also be able to quickly build a go language development environment. If you encounter problems themselves can not solve, godson community provides the official Bug service Network (http://bugs.loongnix.org/login_page.php), you can put the problems encountered, godson team will be in time for you to solve.
Godson Official Bug Submission Platform

http://tieba.baidu.com/p/4653329333

Godson go! Guide to building the Go language environment on godson platform

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.