The first step to learning Go, of course, is to install the Go environment. After all, we can’t do anything without it. But before we install the environment, let’s take a look at the environment variables that Go involves. Here are some of the more common Go environment variables.

GOROOT

The default installation path of Go is c:/ Go in Windows and ~/ Go in Linux/ MacOS

GOPATH

The path to Go source files, package files, and executables; in other words, GOPATH specifies the path to the project-related files

GOOS

The operating system identifier of a computer. For example, Darwin on Mac and Windows on Windows

GOARCH

The processor architecture of a computer, such as 386, ARM, or AMD64

GOBIN

The installation location of the compile file, default $GOROOT/bin

Let’s begin today’s topic, installing the Go environment

The installation process of Go language is very similar to the Java installation process. It can be installed through binary distribution, source code installation, installer, and third-party tools. Source code installation requires a variety of compilation environments, so students with less hands-on skills and interest can use the other three methods

Official Download path

Go language is Google’s internal development of open source language, so, the official website: Golang.org is also provided by Google, but this official website is not so friendly to domestic users, fortunately, Google provides the domestic official website address to be able to visit: Go-zh.org Some information about the Go language for domestic users can be found here. But the site does not provide a download link, just a link to download the page. So let’s just use the download link provided under the golang.cn domain

The download path is golang.google.cn/dl/

Here we only introduce three common ways of installing through binary distributions, installers, and third-party tools. If you are more hands-on, try installing from source code

Install through a binary distribution

The Windows platform

A 64-bit operating system is used as an example

Go the latest version of 1.12.4 download links: dl.google.com/go/go1.12.4…

After downloading it, we unzip it to the default installation directory ‘C:\go’ and then open the control panel to configure the environment variables and put ‘C:\go\bin; ‘to the front of the Path, make sure to exit the environment variable setup window. (If the environment variable does not take effect, try the restart method.)

Open a CMD terminal and type ‘go’. If you see something like the following, the installation is successful. If you do not believe it, you can execute ‘Hello World’ below. If yes, the installation is not successful. You can check the environment variables

Go is a tool for managing Go source code.

Usage:

	go command [arguments]

The commands are:

	build       compile packages and dependencies
	clean       remove object files
	doc         show documentation for package or symbol
	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 more information about a command.

Additional help topics:

	c           calling between Go and C
	buildmode   description of build modes
	filetype    file types
	gopath      GOPATH environment variable
	environment environment variables
	importpath  import path syntax
	packages    description of package lists
	testflag    description of testing flags
	testfunc    description of testing functions

Use "go help [topic]" for more information about that topic.

Copy the code

Linux platform

A 64-bit operating system is used as an example

Go the latest version of 1.12.4 download links: dl.google.com/go/go1.12.4…

Place and unzip the downloaded binary distribution package into the user’s root directory, where the ‘go’ folder will appear containing all of the go installation content

Configuring environment variables in Linux is also relatively easy

Add the following command toHOME/.profile’ will do

export PATH=$PATH:$HOME/go/bin
Copy the code

After the environment variables are configured, enter ‘go’ again and the above success message appears, indicating that the installation was successful

Mac

The installation method of the binary distribution is basically the same as that of the Linux platform. Download the binary distribution package, decompress it to the user root directory, and configure environment variables. Be sure to download the Mac installation package to install it

It is important to note that we did not configure environment variables like ‘GOROOT’,’GOPATH’, etc. when we installed through binary distributions. This is because we install Go at the default location during the installation process. The default location on Windows is ‘C:\ Go ‘, and the default location on MacOS and Linux is ‘$HOME/ Go ‘. So ‘GOROOT’ uses the default path, and ‘GOPATH’ also uses the default path (the user’s root directory), so everything is fine here

Third-party tools

Mac and Linux users use the package manager for program management, Windows does not seem to have (if there is a message to inform you) Go language can also be installed through the package manager, is also very convenient

apt-get

Ubuntu, currently the most used Linux desktop system, uses the apt-get command to manage packages

We can install Go by using the following command

sudo apt-get install golang
Copy the code

It should be noted here that apt does not provide the latest version of Go, and the version of Go downloaded directly through ‘apt-get install golang’ may be relatively low, so we can specify the version to download

Sudo apt - get the install golang - 1.10Copy the code

The latest version of Go provided by Ubuntu16.04 is 1.10, which is not the latest release of Go. If you need to install the latest version, please refer to other installation methods

homebrew

Homebrew is the most used management software tool under the Mac system, brew for short

Brew currently supports Go, which can be installed directly from the command

If you have not installed BREW before, you can refer to the brew official document: brew.sh

brew update && brew upgrade
brew install go
Copy the code

The installation program

Windows and MacOS are the largest desktop systems in the world, and Go also provides an installer to help developers install the Go environment with a simple click of the mouse

If you want to install in this way, you can directly download the corresponding installation program and click run

Test the installation

Now that the environment is installed, we are ready to enter the familiar ‘Hello World’

To do this, copy the following into the main.go document and run the ‘Go run main.go’ test. We’ll talk more about the structure of this file later

package main

import "fmt"

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

Copy the code

If the program runs correctly and prints ‘Hello World’ correctly, congratulations, the Go locale installation has been successful

Follow our “wechat official account”


First official wechat account: Go Technology Stack, ID: GoStack

Copyright belongs to the author, any form of reprint please contact the author.

Author: Search cloud library technology team

Reference: gostack.souyunku.com/2019/04/15/…