Golang, which is the go language, has now been released to 1.4.1 version, the advantages of language features and Google behind the strong backers of what is not said. Golang's official offers a binary installation package on multiple platforms, unfortunately not a binary installation package that does not release the arm platform. The ARM platform is not able to download the binary installation package directly from the official website, fortunately Golang is a multi-platform and open source language, so it can be installed by compiling the source code directly on the arm platform. The whole process includes compiling tool configuration, obtaining Golang source code, setting Golang compiler environment variables, compiling, configuring Golang running environment variables and so on.
Note: This article chooses Raspberry Pi to do the testing, because the Raspberry Pi is based on the arm platform.
1. Compiling tool configuration
It is said that the next version of the Golang compiler tool to use Golang to write themselves, but currently using the C compiler tool. Therefore, the first thing to do is to configure the C compiler tool:
1.1 can be installed on Ubuntu or Debian platform using the sudo apt-get install gcc libc6-dev command, the Raspberry Pi Raspbian system is based on Debian, so it can be installed using this method.
1.2 can be installed on the Redhat or CentOS 6 platform using the sudo yum install gcc libc-devel command.
After installation is complete, you can enter the gcc--version command to verify that the installation was successful.
2. Get Golang Source code
2.1 Download the source code compression package directly from the official website.
Golang Official website provides Golang source code compression package, can be downloaded directly, the latest 1.4.1 Version source codes link: https://storage.googleapis.com/golang/go1.4.1.src.tar.gz
2.2 Using the Git tool.
Golang uses GIT version management tools, or you can use Git to get Golang source code. It is recommended to use this method because you can always get the latest Golang source code at any time.
2.2.1 First confirm that the Git tool is already installed on the arm platform and can be confirmed using the git--version command. The general Linux platform has git installed, you can install it by yourself,不同平台的安装方法可以参考:http://git-scm.com/download/linux
2.2.2 Cloning a remote Golang git repository to a local
In the terminal CD to the directory where you want to install Golang, make sure that there is no directory named go in this directory. The following command then obtains the code warehouse:
git clone https://go.googlesource.com/go
The mainland region may fail, and I have tried several times without turning over the wall for reasons that we all know. Fortunately, Google has also hosted Golang on GitHub, so you can also get it by using the following command:
git clone https://github.com/golang/go.git
Depending on the network situation, the download may take some time. My 2M bandwidth took nearly two hours to download, although the entire project was only a few 10 trillion =
After the download is complete, you can see that there is a go directory in the directory, which is the source code for Golang, the CD GO command on the terminal to enter the directory.
Execute the following command to check out the go1.4.1 version of the source code, because the new code has now been submitted, the latest code may not be the most stable:
git checkout go1.4.1
At this point, the latest 1.4.1 release of the source code has been obtained
3, set the GOLANG environment variable
The main goroot, GOOS, Goarch, goarm Four environment variables need to be set, first explain the meaning of four environment variables.
3.1 Goroot
The main representative is the path to the Golang tree structure directory, which is the go directory that git checked out above. It is generally not necessary to set this environment variable, because when compiling, the default is to use the All.bash script in the SRC subdirectory of the Go directory as the value of the Goroot when running the parent directory. For insurance purposes, you can set the path directly to the Go directory.
3.2 Goos and Goarch
Represents the compiled target system and platform, with the following optional values:
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 |
It is important to note that these two values represent the target system and platform, not the system and platform that compiles the source code. The Raspberry Pi Raspbian is a Linux system, so these goos are set to Linux,goarch set to arm.
3.3 Goarm
Represents the use of the floating-point operation coprocessor version number, which is only useful for arm platforms and has an optional value of 5,6,7. If the source code is compiled on the target platform, this value can not be set and it will automatically determine which version to use.
To summarize, set the Golang compiler environment variable on the Raspberry Pi, edit the $HOME/.BASHRC file and add the following at the end:
Export goroot= your go directory path to export Goos=linuxexport Goarch=arm
Save after editing, and execute the source ~/.BASHRC command to make the changes take effect.
4. Compiling source code
The environment variable configuration is complete and you can start compiling the source code. In the SRC subdirectory of the Go directory, there are mainly All.bash and make.bash two scripts (plus two all.bat and Make.bat scripts for the window platform). Compiling actually executes one of the scripts, the difference being that All.bash executes some test suites after the compilation is complete. If you want to compile only without testing, you can run the Make.bash script. Use the CD command to enter the go down src directory, execute the ./all.bash or ./make.bash command to start compiling. Compilation takes a different amount of time due to different hardware conditions. It took nearly half an hour to compile my B-Raspberry Pi, and it took about one hours for the test suite to execute after compiling, and it took about 1.5 hours to complete.
5. Configure Golang Run environment variable
When the compilation is complete, the Go directory will generate the bin directory, which is the run script of Go. For later use, you can add this bin path to the PATH environment variable. Also edit the ~/.BASHRC file because the GOROOT environment variable is previously set to the Go directory, so just add it at the end
Export path= $PATH: $GOROOT/bin
After saving the same, execute the source ~/.BASHRC command to make the environment variable effective.
At this point, the Golang source code is compiled and installed successfully. Executing go version should be able to see the current version of the Golang, indicating that the compilation installation was successful.
There is also a more important gopath environment variable needs to be set, and so on time to talk about it.
Reference Official Document: Https://golang.org/doc/install/source
Compiling and installing Golang on the arm platform