Linux under Golang installation configuration tutorial

Source: Internet
Author: User
Tags install go mercurial virtual environment

As a transport personnel, the use of Python language is sufficient, in theory, do not need to understand Golang. However, a recent discovery of a trendy monitoring system is based on Golang, such as the Millet Company's Open-falcon (GitHub project page), the stack Exchange company Bosun (GitHub project page).

First, go three kinds of installation methods

Go has a variety of ways to install, you can choose what you like. Here are three of the most common ways to install:

Go source installation: This is a standard way to install software. For users who often use Unix-like systems, especially for developers, you can customize them from the source installation.
Go Standard Pack Installation: Go provides a convenient installation package, supporting Windows, Linux, Mac and other systems. This way is suitable for quick installation, can download the corresponding installation package according to own system bit number, the next can install easily. Recommend this way
Third-party tool installation: There are many convenient Third-party package tools, such as Ubuntu Apt-get, Mac homebrew and so on. This installation method is suitable for those who are familiar with the corresponding system.
Finally, if you want to install more than one version of Go in the same system, you can refer to the Third-party tool GVM, which is currently the best tool in this area, unless you know what to do with it.

Second, go source installation

In the go source code, some parts are written in Plan 9 C and at&t, so if you want to install from the source, you must install the C compiler tool.

In the Mac system, as long as you install the Xcode, it already contains the corresponding compiler tool.

In Unix-like systems, tools such as GCC need to be installed. For example, the Ubuntu system can install the compilation tool by performing sudo apt-get install gcc libc6-dev in the terminal.

In the Windows system, you need to install MinGW, then install GCC via MinGW and set the appropriate environment variables.

Because I usually mainly related to the use of Linux platform, so here only to Linux platform as an example, does not involve Mac and win under the installation.

1, go source code download

From the official page, the domestic mirror or the github download go to your computer, and then the unpacked directory to go through the 1 command to move to $goroot point of location.

#本人设置 $GOROOT for/usr/local/go
Export $GOROOT =/usr/local/go
wget https://storage.googleapis.com/golang/go<VERSION>.src.tar.gz
Tar zxv go<version>.src.tar.gz
MV Go $GOROOT

2, install GCC and other compiling tools

sudo apt-get install bison ed gawk gcc Libc6-dev make
Or
Yum GCC Mak gawk

Depending on the distribution, choose the appropriate compilation environment package installation.

3, compile

CD $GOROOT/SRC
./all.bash

4. Environment variable Configuration

Export Goroot=/usr/local/go
Export path= $PATH: $GOROOT/bin
or use the following:
Export path= $PATH:/usr/local/go/bin
Depending on your needs, you can configure the appropriate environment variables in any file, such as. BASHRC,. Profile,/etc/profile, and so on.

5, testing

Run the GO command directly, and the following section appears, indicating that the environment is well installed.

[Root@361way ~]# Go
Go's a tool for managing go source code.
Usage:
Go command [arguments]
The commands are:
Build compile packages and dependencies
Clean Remove object files
ENV Print GO Environment information
Fix run Go tool fix on packages
FMT run GOFMT on package sources
Generate generate Go files by processing source
Get download and install packages and dependencies
Install compile and install packages and dependencies
List List Packages
Run compile and run go program
Test test Packages
Tool run specified Go tool
Version Print Go version
Vet Run Go tool vet on packages
Use ' Go help [command] ' for the more information about a command.
Additional Help Topics:
C Calling between Go and C
filetype file types
Gopath gopath environment variable
Importpath Import Path syntax
Packages Description of package lists
Testflag Description of testing flags
TestFunc Description of testing functions
Use ' Go help [topic] ' for the more information about that topic.

Third, go standard package installation

Through the official compression package download, the standard package contains the source code, but the difference is that it has been unpacked after the corresponding bin directory exists, which has already compiled a good Golang binary package. You can configure the environment variables directly using the operations in part 4th above to take effect.

Iv. installation of third party tools

1. Under the CentOS platform

Need to use Third-party source Epel, as follows:

Yum-y Install Golang

2. Under the Ubuntu platform

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:gophers/go
sudo apt-get update
sudo apt-get install golang-stable git-core Mercurial

3, under the Mac platform

Brew Update && Brew upgrade
Brew Install Go
Brew Install Git
Brew Install Mercurial

Five, GVM tools

GVM is a Golang virtual environment Configuration tool that allows multiple versions of Golang to be installed on a single machine, and GVM is a third-party-developed go version management tool, similar to the RVM tool in Ruby. It is quite convenient to use, installation GVM use the following command:

Bash < < (curl-s-s-l Https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
Once the installation is complete, we can install go:

GVM Install go1.4.2
GVM Use go1.4.2
You can also use the following command to omit the hassle of GVM using each call:
GVM use go1.4.2--default after the execution of the above command Gopath, goroot and other environment variables will automatically set up, so you can be used directly.

Six, GO environment variables

The Go development environment is dependent on some operating system environment variables, and you'd better set them up between going to setup. If you are using Windows, you do not have to manually set up, go will be installed by default in the directory C:/go. Here are some of the most important environment variables:

$GOROOT indicates where go is installed on your computer, its value is generally $home/go, and of course you can install it somewhere else.
The $GOARCH represents the processor architecture of the target machine, and its value can be 386, AMD64, or arm.
The $GOOS represents the operating system of the target machine, and its value can be Darwin, FreeBSD, Linux, or Windows.
$GOBIN represents the location of the compiler and linker installation, default is $goroot/bin, if you are using Go 1.0.3 and later versions, you can normally set its value to null and go will use the default value mentioned previously.
The target machine is the machine where you plan to run your go application.

The Go compiler supports cross compilation, which means that you can build applications running on a single machine that run on different operating systems and processor architectures, meaning that the machines that write the source code can have completely different characteristics from the target machine (operating system and processor architecture).

To differentiate between the local machine and the target machine, you can use $gohostos and $gohostarch to set the parameters of the target machine, which will only be used for cross compilation, and if you do not display settings, their values will be the same as the local machine ($GOOS and $goarch).

The $GOPATH defaults to the same value as $goroot, but starting with the Go 1.1 version, you must modify to another path. It can contain multiple paths containing the go language source files, package files, and executables, which must contain three specified directories: SRC, pkg, and bin, which are used to store source files, package files, and executables, respectively.
$GOARM specifically for a processor based on the ARM architecture, its value can be 5 or 6, and the default is 6.
$GOMAXPROCS is used to set the number and number of processors that the application can use, as detailed in section 14th. 1.3.
In the next sections, we'll discuss how to install the Go language on Linux, Mac OS X, and Windows. Installation on the FreeBSD and
Linux is very similar. The development team is trying to migrate the go language to other examples such as OpenBSD, Dragonflybsd, NetBSD, plan
9, Haiku and Solaris operating systems, you can find the most recent developments on this page: Go porting efforts.

Examples of host environment usage:

Export GOARCH=AMD64
Export Goos=linux

Seven, Hello Word

Create a Hello.go file that reads as follows:

Package Main
Import "FMT"
Func Main () {
Fmt. Printf ("Hello, world\n")
}

The results of the operation are as follows:

# Go Run hello.go
Hello, world

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.