Compile and install Golang on the ARM platform

Source: Internet
Author: User

Compile and install Golang on the ARM platform

Golang, also known as the Go language, has now been released to version 1.4.1. the superiority of language features and the powerful reliance on Google are not much to say. Golang officially provides binary installation packages on multiple platforms. Unfortunately, the binary installation packages on ARM platforms are not released. The ARM platform cannot directly download the binary installation package from the official website. Fortunately, Golang supports multiple platforms and open-source languages. Therefore, you can directly compile the source code on the ARM platform for installation. The entire process mainly includes compilation tool configuration, obtaining Golang source code, setting Golang compilation environment variables, compiling, and configuring Golang line environment variables.

Note: Raspberry Pi is used for testing in this article because Raspberry Pi is based on the ARM platform.

1. Compile tool Configuration

It is said that the next version of golang compilation tool should be written by golang, but the C compilation tool is still used. Therefore, you must first configure the C compilation tool:

1.1 on Ubuntu or Debian platform, you can use the sudo apt-get install gcc libc6-dev command to install. Raspberry Pi's RaspBian system is modified Based On Debian, so you can use this method to install.

1.2 you can use the sudo yum install gcc libc-devel command on RedHat or CentOS 6.

After the installation is complete, enter the gcc -- version command to verify whether the installation is successful.

2. Obtain golang source code

2.1 Download the source code package directly from the official website.

Golang official website provides golang source code compressed package, can be directly downloaded, the latest version of 1.4.1 source code link: https://storage.googleapis.com/golang/go1.4.1.src.tar.gz

2.2 Use the git tool.

Golang uses the git version management tool or git to obtain the golang source code. This method is recommended because you can obtain the latest golang source code at any time.

2.2.1 first confirm that the git tool has been installed on the ARM platform. You can use the git -- version command to confirm. General linux platform installed git, if not, you can install on your own, the installation method of different platforms can refer to: http://git-scm.com/download/linux

2.2.2 clone the remote golang git repository to the Local Machine

On the terminal cd to the directory where you want to install golang, make sure that there is no directory named go under this directory. Run the following command to obtain the code repository:

Git clone https://go.googlesource.com/go

It may fail to be obtained in mainland China. I tried it several times without turning over the wall, because everyone understands it. Fortunately, google has hosted golang on github, so it can also be obtained through the following command:

Git clone https://github.com/golang/go.git

Depending on the network, the download may take a long time. It took me nearly two hours to download the 2 M bandwidth, although the entire project was just dozens of megabytes =

After the download is complete, you can see that there is an additional go directory under the Directory, which is the golang source code. Execute the cd go command on the terminal to enter the directory.

Run the following command to check out the source code of go1.4.1. Because New Code has been submitted, the latest Code may not be the most stable:

Git checkout go1.4.1

So far, the source code of the latest 1.4.1 release has been obtained.

3. Set golang's compiling environment variable
It mainly includes four environment variables: GOROOT, GOOS, GOARCH, and GOARM. The meanings of these four environment variables must be explained first.

3.1 GOROOT

It mainly represents the path of the golang tree directory, that is, the go directory detected by git above. Generally, you do not need to set this environment variable because the parent directory of the all. bash script in the src subdirectory of the go directory is used as the value of GOROOT by default during compilation. To be safe, you can directly set the path to the go directory.

3.2 GOOS and GOARCH

It represents the compiled target system and platform respectively. The optional values are as follows:

GOOS GOARCH
Darwin 386
Darwin Amd64
Dragonfly 386
Dragonfly Amd64
Freebsd 386
Freebsd Amd64
Freebsd Arm
Linux 386
Linux Amd64
Linux Arm
Netbsd 386
Netbsd Amd64
Netbsd Arm
Openbsd 386
Openbsd Amd64
Plan9 386
Plan9 Amd64
Solaris Amd64
Windows 386
Windows Amd64

Note that these two values represent the target system and platform, rather than the system and platform for compiling source code. RaspBian of Raspberry Pi is a linux system, so these GOOS are set to linux and GOARCH is set to arm.

3.3 GOARM
Indicates the version number of the floating-point coprocessor used. It is only applicable to the arm platform and has optional values: 5, 6, and 7. If the source code is compiled on the target platform, this value can be left unspecified and it automatically determines which version is needed.

In conclusion, set golang's compiling environment variable on Raspberry Pi. Edit the $ HOME/. bashrc file and add the following content at the end:

Export GOROOT = Your go directory path
Export GOOS = linux
Export GOARCH = arm

Save the edited files and execute source ~ /. Bashrc command to make the modification take effect.

4. Compile source code

After environment variable configuration, you can start to compile the source code. In the src sub-directory under the go directory, there are mainly two scripts: all. bash and make. bash (and two other all. bat and make. bat scripts apply to the window platform ). Compiling is actually executing one of the scripts. The difference between the two is that all. bash will execute some test suites after compilation. Run the make. bash Script If You Want To compile but not test it. Run the cd command to enter the src directory under go and run the./all. bash or./make. bash command to start compilation. The compilation time varies depending on different hardware conditions. It took nearly half an hour to compile my B-type Raspberry Pi. After compilation, it took about one hour to execute the test suite. It took about one and a half hours.

5. Configure golang Runtime Environment Variables

After compilation, the bin directory is generated under the go Directory, which contains the running script of go. For future use, you can add this bin path to the path environment variable. Also edit ~ /. Bashrc file. Because the GOROOT environment variable is set to point to the go directory, you only need to add

Export PATH = $ PATH: $ GOROOT/bin

Save and execute source ~ /. Bashrc command to make the environment variable take effect.

So far, golang source code compilation and installation are successful. Execute go version to view the current golang version information, indicating that the compilation and installation are successful.

Another important GOPATH environment variable needs to be set, as shown in.

Reference official documents: https://golang.org/doc/install/source

Getting started with Linux-installing Go in Linux

Install Go Language Pack in Ubuntu

Go language programming (HD) full-version ebook

Go language and beauty-surpassing "Hello World"

Why do I like the Go language?

Implementation of the Go language memory distributor

This article permanently updates the link address:

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.