Following the systematic introduction of GO, the next step into go development environment building series. Writing code in any language requires two tools: language development kits and code editing tools.

Today, let’s talk about how to install Golang. Maybe we all think it’s too simple to write an article about. That’s what I thought at first. But after further understanding, I gradually feel that this is also a very interesting thing.

How to install

Similar to installation in other languages, go can be installed in three ways, from simple to complex: system installation, official binary package installation, and source code compilation installation.

System way

Different operating systems usually provide a way to install software for GO. This method greatly simplifies the installation process and saves us some complicated steps. The following describes the installation methods in different systems:

windows

On Windows, software installation is usually done by downloading software packages like Setup.exe/MSI. Follow the navigation prompts, continuously perform “Next” “Next” to complete. If you visit the local address, you will see the following:

Select “Microsoft Windows” to download the Windows installation package. Today’s systems are basically 64-bit, so you generally don’t have to worry about 32/64-bit systems.

Download the installation package, click start execution, the next step is to follow the navigation prompts step by step. One thing to note is that GO is installed in C:\GO by default. If you want to change the default installation path, select it again when you see the screen below.

ubuntu/debian

On Debian or Ubuntu, you can install Go using the apt-get command. For example, on Ubuntu 16.04.5 LTS, use the following command to install:

Sudo apt-get update sudo apt-get install golang-goCopy the code

For a newly created system, update the software source. Otherwise, the installation may fail due to some source exceptions.

centos/redhat

On centos or Redhat, you can install Go using the yum command. For example, on CentOS 7.5, run the following command:

$ yum epel-release
$ yum install golang
Copy the code

The epel-Releaes source was downloaded to prevent golang installation from being unsupported or too old for Yum.

macos

On MacOS, you can install Go using a PKG file or Homebrew.

The installation method of PKG is similar to that of Windows setup.exe/ MSI. Download the software and press the navigation “Next” “Next” to complete.

How to use Homebrew installation. Unlike yum and apt-get, Homebrew does not come with the MAC system and requires installation. Go to the homebrew official website and the top of the page explains how to install homebrew. The command is as follows:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Copy the code

To install go, run the following command:

$ brew install go
Copy the code

The installation is very simple.

The advantage of system installation is simplicity. The disadvantage is that we cannot guarantee that the version provided by the system will meet our requirements. For example, the go version of Ubuntu installed above is relatively low, which is GO1.6.

Binary package

Next, how to install using binary packages. Binary packages are packages that have already been compiled. This installation process is similar on different operating systems, but considering that Windows has the most users, let’s take Windows as an example.

Enter the download page again, in the list can be as follows. Because I use a 32-bit Windows virtual machine, download the i386 package.

Then unzip the downloaded package into a folder, such as C :\Program Files, and you will find that it already contains a new folder named go.

At this point, the binary package installation is complete. Since binary packages are compiled packages, the corresponding packages need to be downloaded for different systems and CPU architectures.

We might think, just move a folder? Yes, the system installation is actually to do these things, the difference is that the system installation in the simplified operation at the same time will also be targeted to do some Settings, such as configuring environment variables, file classification storage, etc..

The source code to compile

The advantage of this way of installation is that it has nothing to do with the system, all the control is in their own body, can limit us only our own ability.

As mentioned in the previous article, go has removed all C code from the source code in version 1.5 and implemented self-compilation. Therefore, we can use the system existing GO to compile the source code, so as to achieve a new version of the installation.

In ubuntu, I used apt-get to install the older version of Golang go1.6. Let’s use it to compile a new version of Go.

Download the source code, the latest version of the source code can be downloaded by clicking on go1.12.2.src.tar.gz. The source code for Go is hosted on Github at github.com/golang/go. If you want to try out the new feature in advance, you can pull the latest code from Github and compile it. This is also a benefit of source code compilation and installation.

Download the source code after the completion of the source directory can be compiled. Note that if you compile using a virtual machine, make sure you have enough memory.

$tar ZXVF go1.12.2.src.tarcd go/src
$ ./all.sh
Copy the code

Execute./all.sh to complete the compilation and installation, which is also quite simple. This process will take time. It will take time. This simplifies a lot of details, but if you want to take a closer look, you can read the official documentation install Go from Source.

The environment variable

After installing Golang, there are three environment variables you need to know: GOROOT, GOPATH, and PATH. Here’s what they do.

GOROOT

Root directory of the GO installation. This variable needs to be handled differently in different versions.

Prior to GO 1.10, we needed to decide whether to manually configure depending on the installation mode. Such as source code compilation installation, installation will have default Settings. In Windows, C:\GO is recommended. In Linux, freeBSD, and OS X, /usr/local/go is recommended. If you want to customize the installation location, you must manually set Up GOROOT. All this has been taken care of for us if we install it in a system way.

For this topic, I recommend reading you-dont-need-to-set-goroot and analyzing the source code for installing go.

In GO 1.10 and beyond, this variable is no longer needed and dynamically determines the GOROOT value based on the GO toolset location relative to the GO tool. Simply put, the go command determines the location of GOROOT.

Executable to find GOROOT and Github Go Issues 25002 for this topic.

PATH

An environment variable exists in each operating system. It is used to specify the default search path for the system to execute commands. The command is executed without writing the full path.

On Windows, for example, I installed go in C: Program Files\go, so GOROOT is C: Program Files\go, then the PATH variable can be appended to C: Program Files\go bin.

GOPATH

For those of you who know Python, this is analogous to python’s environment variable PYTHONPATH, which sets our working directory, where we write our code. Packages are also found in the path set by GOPATH.

Prior to Go 1.8, this variable had to be set manually. Go 1.8 and later, if not set, default to $HOME/go, the go directory in your user directory.

How to set up

After introducing the three variables, take my MAC as an example to introduce the setting method.

Unix-like system environment variables are set similarly. Use the export command to set the environment variables and add the commands to /etc/profile, which is executed with the shell console turned on. Specific operation commands are as follows:

$ sudo vim /etc/profile
...
export GOROOT=/usr/local/go // Do not set the default location, or after version 1.10export PATH=$PATH:$GOROOT/bin
exportGOPATH=/Users/polo/work/go // Multiple directories can be setCopy the code

After the above steps, environment variables are configured. To enable environment variables immediately, we need to restart the console. Then we can use go env to see how the variable is configured.

$ go env
GOARCH="amd64"
GOBIN="/usr/local/go/bin". GOPATH="/Users/polo/Public/Work/go". GOROOT="/usr/local/go"
Copy the code

The directory structure

A few more words about go’s directory structure. For Windows, enter C: Program Files\ Go and you will see the following.

Introduce some of the main directories:

  • API, which contains a list of all apis, as if the IDE uses the information in it;
  • Bin, which contains some go tool commands, mainly go, gofmt, godoc, command usage method will be introduced later
  • Doc, go usage documents, so that we can read without the Internet;
  • SRC, the source code for golang’s compiler, various toolsets, and standard libraries,

An introduction to case

That’s it. Let’s take a simple example, the classic Hello World.

First, create a file named Hello. go with the suffix. Go as follows:

package main

import "fmt"

func main(){
    fmt.Println("Hello World")}Copy the code

The above code mainly consists of several parts, respectively

  • The file in Go must belong to a package. Main is special and is where the program entry is located.
  • Import “FMT” to import the FMT package, which is a way to import the package and then use the function variables provided by FMT;
  • Func main() {}, func keyword function definition, main is the function name, in the main package is the entry of the program;
  • FMT.Println, a block of code in main that calls the Println function provided by FMT to print the string “Hello World”

Next, we can use go Run to execute this code as follows:

$ go run hello.go
Hello World
Copy the code

Execute the output “Hello World”.

conclusion

This article describes how golang can be installed in various scenarios from different systems and approaches. It then detailed several environment variables commonly used in GO and ended with a simple example to complete the go installation.