How to build a Go 1.5 development environment from source code

Source: Internet
Author: User
Tags install go
This is a creation in Article, where the information may have evolved or changed.

Please note that the text of this article contains a number of links. If it is reproduced or use some of the reader does not support hyperlinks, please do your own brain repair.

In the recent past, go Team has released several major introductions to the groundbreaking version of Go 1.5, which will be unveiled in August. Rob and Andrew detail the important features and details that appear in Go 1.5, respectively, in go and the state of Go. The main change in this version is the removal of all C code, either runtime or compiler, using the Go language and a small subset of the assembly-that's what people often say about bootstrap. But this also means that go in 1.5 and later versions, using source code to build go
The development environment will face the trouble of "chicken eggs, eggs and chickens" (of course, if you directly "buy eggs"-Using the binary installer package-there is no problem).

In Go1.4 and earlier versions, GCC was used to compile a small compiler that was written in C with only basic functionality as the boot tool for building the GO environment. This means that C-related tools such as GCC and make must be installed to build the Go development environment from source code. According to Rob's handout and his written "Go 1.5 Bootstrap Plan", go 1.5 will no longer have C-language participation, but need to use the Go 1.4 version of the tool chain to compile. It also means that starting from the source code to build go 1.5 requires two versions to coexist. A few years ago, many people were goroot/gopath in the pits. Now you need two more versions of Go co-exist, it seems to be a very troublesome thing to think about.

For Go, the Boulevard to Jane! So through this article I would like to briefly explain how to build the Go 1.5 development environment using source code. Because of the "chicken eggs, eggs and chickens", we need to start from the development environment to build Go 1.4.

It should be stated that all of the following are demonstrated in Ubuntu 14.04, but as long as it is a POSIX compliant system, the following actions should be consistent. Users of Windows I strongly recommend that you use a binary package for installation. No toss!

Preparatory work

A "clean" system is required, where "clean" refers to environment variables that have not been set to Goroot/gopath/gobin. If you have previously configured the GO environment, you can only adjust or delete the reset as appropriate.

Also, because you need to compile Go 1.4, you must install the C related tools:

$ apt-get Install gcc Libc6-dev

# #目录结构

In 2012, I translated an article "GO Environment settings". Although the code of Go at that time is also using HG for version control, and go 1 is not officially released, but that article described in the directory settings, I have been used so far, its structure is as follows:

$HOME/golang/├──3rdpkg├──go└──own

where $HOME/golang/3rdpkg , $HOME/golang/go and $HOME/golang/own directories should be included GOPATH in the environment variables in order. The benefit is that the go get first path that is directly imported to the go package when you use it is the GOPATH 3rdpkg subdirectory. This separates the third-party package, Go code, and your working directory.

However, since Go 1.5 requires two versions of Go coexistence, the directory structure needs to be adjusted accordingly. Finally, I'll explain in more detail later.

Environment variables

The settings for Gopath have been mentioned earlier:

Gopath= $HOME/golang/3rdpkg: $HOME/golang/go: $HOME/golang/own

Since there are now two versions of the Go code that coexist, we need to create a soft link to the desired version of the code directory, for example:

/golang/├──3rdpkg├──go-Go1.4/├──go1.4└──own, $HOME

Thus, the value of Goroot should be set to:

goroot= $HOME/golang/go

These two environment variables are sufficient (cross-compilation and environmental trimming are not covered in this article).

In order to be able to use the GO command conveniently, you will also need to $GOROOT/bin/ join the PATH:

Path= $PATH: $GOROOT/bin/

Install Go 1.4

Use the git command to get the full code for Go 1.4. Currently the latest 1.4 version is 1.4.2, so:

$ cd $HOME/golang/$ git clone-b go1.4.2 https://github.com/golang/go.git go1.4

Then let Goroot's soft-link directory point to the directory where the Go 1.4 code is actually saved:

$ ln-s go1.4 Go

At this point the directory structure is:

/golang/├──3rdpkg├──go-Go1.4/├──go1.4└──own, $HOME

The value of the environment variable is:

Gopath= $HOME/golang/3rdpkg: $HOME/golang/go: $HOME/golang/owngoroot= $HOME/golang/gopath= $PATH: $GOROOT/bin

Go $HOME/golang/go/src to the directory and run the All.bash script.

CD $HOME/golang/go/src./all.bash

After a short compilation and a lengthy test, go 1.4 should be deployed.

Use go version the command to see that the current Go version is 1.4.2:

$ go version go1.4.2 LINUX/AMD64

Install Go 1.5

Since go 1.5 needs to be built based on Go 1.4, go 1.5 requires a separate directory placement (in fact it is possible to use the same directory, but it requires a lot of extra setup, no tossing).

Since the code base of Go has been cloned, you can copy go1.4 directory to directory go1.5 directly, then use the command:

$ go checkout-b Master

Switch to the code branch where Go 1.5 is located.

One thing to note: since Go 1.5 is expected to be officially released by August, there will be no go1.5 this tag until then. Therefore, the current Master branch is actually the code branch of the Go 1.5 feature freeze.

Since Goroot is set to, it is $HOME/golang/go only necessary to re-point the soft link to the directory where the Go 1.5 code is located, without modifying the environment variables.

$ cd $HOME/golang$ unlink go$ ln-s go1.5 Go

In order to be able to compile go 1.5, you also need to set up an additional called goroot_bootstrap environment variable, pointing to the directory where go 1.4 resides. In order to be backwards compatible, the variable also uses a soft link to point to:

$ ln-s go1.4 Go-bootstrap

At this point the directory structure is:

Go1.4/├──go1.4├──go1.5└──own/golang/├──3rdpkg├──go, Go1.5/├──go-bootstrap, $HOME

The value of the environment variable is:

Gopath= $HOME/golang/3rdpkg: $HOME/golang/go: $HOME/golang/owngoroot= $HOME/golang/gopath= $PATH: $GOROOT/bingoroot_ bootstrap= $HOME/golang/go-bootstrap

Go $HOME/golang/go/src to the directory and run the All.bash script.

$ cd $HOME/golang/go/src$./all.bash

After another brief compilation and a lengthy test, go 1.5 should be deployed.

Use go version the command to see the current Go version as the development number:

Go version devel +6551803 Wed 04:48:29 +0000 linux/amd64

For developers who keep track of the Go code base, maintaining such a directory structure can make future upgrades easier. Just create a new version of the Code directory, adjust the point of the soft link, and then compile to get a new version of the development environment.

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.