Install Go language and build Go language development environment

Go language installation Go language syntax is similar to C, but it has the following features: memory safety, GC (garbage collection), structural form and CSP-style concurrent computing. The following is a brief introduction to the installation of Go language and the establishment of the Go language development environment.

What is Go

Go is a computer programming language. Go (also known as Golang) is a statically strongly typed, compiled language developed by Robert Griesemer, Rob Pike and Ken Thompson of Google. The syntax of Go is similar to C, but it has the following features: memory safety, GC (garbage collection), structural form and CSP-style concurrent computing.

The following is a brief introduction to installing the Go language and building.

Go language download address

Go official website download address:https://golang.org/dl/

Go official mirror site (recommended):https://golang.google.cn/dl/

Version selection

It is recommended to download the executable file version for Windows and Mac platforms, and the compressed file version for Linux platforms. Go language updates and iterations are relatively fast, so it is recommended to use a newer version to experience the latest features.

-1

Install

Windows Installation

This installation example uses 64-bit Windows 10System Installation Go1.14.1 executable versionFor example.

Download the installation package selected in the previous step to your local computer.

-2

Double-click the downloaded file and follow the steps below to install it.

-3-4-5-6-7

Installation on Linux

If you don't want to write Go code on the Linux platform, you don't need to install Go on the Linux platform. The Go code written on our development machine only needs to be cross-platform compiled (see cross-platform compilation at the end of the article for details) and then it can be copied to the Linux server for execution. This is also the advantage of Go programs being easy to deploy across platforms.

We select and download on the version selection pagego1.14.1.linux-amd64.tar.gzdocument:

wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz

Unzip the downloaded file to/usr/localUnder the directory:

tar -zxvf go1.14.1.linux-amd64.tar.gz -C /usr/local  # decompression

If you are prompted that you do not have permission, addsudoRun it again as root user. After execution, you can/usr/local/See belowgoDirectory.

Configure environment variables: There are two files in Linux that can configure environment variables,/etc/profileIt is effective for all users;$HOME/.profileIt is effective for the current user. Select a file to open according to your own situation, add the following two lines of code, save and exit.

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

Revise/etc/profileAfter the reboot takes effect, modify$HOME/.profileThen use the source command to load$HOME/.profileThe file will take effect. Check:

~ go version go version go1.14.1 linux/amd64

Installation on Mac

Download the executable file version and clickNext stepJust install it. By default, go will be installed to/usr/local/godirectory.-8

examine

After the previous installation process is completed, you can open the terminal window and entergo versionCommand to view the installed Go version.-9

GOROOT and GOPATH

GOROOTandGOPATHare all environment variables, among whichGOROOTis the path where we install the go development package. Starting from Go 1.8, the Go development package will be installed asGOPATHSet a default directory, and after enabling the Go Module mode in Go 1.14 and later versions, you don't have to write your code to the GOPATH directory, soWe don't need to configure GOPATH ourselvesJust use the default one.

GOPROXY is very important

After Go1.14, it is recommended to usego modmode to manage the dependent environment, and no longer force us to write code inGOPATHThe src directory below, you can write go code anywhere on your computer. (Some tutorials online are applicable to versions before 1.11.)

The default GoPROXY configuration is:GOPROXY=https://proxy.golang.org,direct, due to the lack of domestic accesshttps://proxy.golang.org, so we need to change a PROXY, it is recommended to usehttps://goproxy.ioorhttps://goproxy.cn.

You can modify GOPROXY by executing the following command:

go env -w GOPROXY=https://goproxy.cn,direct

Go Development Editor

Go uses UTF-8 encoded text files to store source code. In theory, any text editor can be used for Go language development. Here we recommend usingVS CodeandGolandVS CodeIt is an open source editor from Microsoft, andGolandIt is a paid IDE produced by jetbrains.

We use hereVS Code Add plugins as development tools for the go language.

Introduction to VS Code

VS CodeFull nameVisual Studio Code, is an open source software developed by MicrosoftfreeA modern lightweight code editor that supports syntax highlighting, intelligent code completion, custom hotkeys, bracket matching, code snippets, code comparison Diff, GIT and other features of almost all mainstream development languages. It supports plug-in extensions and supports Win, Mac and Linux platforms.

Although it is not as powerful as some IDEs, it is sufficient for our daily Go development after adding the Go extension plug-in.

Download and Installation

VS CodeOfficial download address:https://code.visualstudio.com/Download

All three major mainstream platforms are supported. Please choose the corresponding installation package according to your computer platform.-10Double-click the downloaded installation file to install it.

Configuration

Install the Simplified Chinese plugin

Click the last item in the left menu barManaging Extensions,existSearch BoxInputchinese , select the first item in the result list, clickinstallInstall.

After the installation is complete, a prompt will appear in the lower right cornerRestart VS CodeAfter restarting, your VS Code will display Chinese!-11VSCodeMain interface introduction:-12

Install the Go extension

Now we have to install our VS Code editorGoExtend the plug-in to support Go language development.-13

First Go Program

Hello World

Now let's create our first Go project -hello. Create ahelloTable of contents.

go mod init

When creating a new project using the go module mode, we need to passgo mod init project nameThe command is used to initialize the project. This command will be generated in the project root directory.go.modFor example, we usehelloAs the name of our first Go project, execute the following command.

go mod init hello

Writing Code

Next, create amain.godocument:

package main  //Declare the main package, indicating that the current program is an executable program

import "fmt"  // Import the built-in fmt package

func main(){  // The main function is the entry point for program execution
	fmt.Println("Hello World!")  // Print Hello World! to the terminal!
}

Very important!!! If a prompt pops up in the lower right corner of VS Code asking you to install the plugin, be sure to click install all to install it.

Compile

go buildThe command compiles source code into an executable file.

Execute in the hello directory:

go build

Or execute the following command in another directory:

go build hello

The go compiler will go GOPATHFind the one you want to compile in the src directoryhelloproject

The compiled executable file will be saved in the current directory where the compilation command is executed. If it is a Windows platform, it will be found in the current directoryhello.exeExecutable file.

You can execute this command directly in the terminal.hello.exedocument:

c:\desktop\hello>hello.exe Hello World!

We can also use-oParameter to specify the name of the executable file after compilation.

go build -o heiheihei.exe

VSCode switches cmd.exe as the default terminal under Windows

If you open the terminal interface of VS Code and the following scene appears (pay attention to the part in the red box), then yourVS CodeCurrently usingPowerShellAs default terminal:-14It is highly recommended that you follow the steps below and selectcmd.exeAs the default terminal tool:-15At this point, the following interface will pop up in the middle of the top of VS Code. Refer to the figure below and move the mouse to select the suffixcmd.exeof that one and click the left mouse button.

at lastRestart the terminal that is already opened in VS CodeorRestart VS Code directlyThat's it.-16If the drop-down triangle does not appear, it does not matter. PressCtrl+Shift+PA box will appear above VS Code. Enter the following information:shell, then click the specified option to display the above interface.-17

go run

go run main.goYou can also execute programs, which essentially compiles first and then executes.

go install

go installIt means installation. It first compiles the source code to get the executable file, and then moves the executable file toGOPATHIn the bin directory. Because our environment variables are configuredGOPATHThe bin directory under , so we can execute the executable file directly from anywhere.

Cross-platform compilation

By default wego buildThe executable files are all executable files of the current operating system. The Go language supports cross-platform compilation - compiling executable files of other platforms (such as Linux) under the current platform (such as Windows).

Compiling Linux executable files on Windows

If I want to compile an executable file for Linux under Windows, what should I do? I just need to specify the platform and processor architecture of the target operating system when compiling.

Note: Whether you use VsCode editor or Goland editor on Windows computer, you should pay attention to the type of terminal you use, because different terminals have different commands!!! Currently, Windows usually uses the defaultPowerShellterminal.

If yourWindowsThe one used iscmd, then specify the environment variables as follows.

SET CGO_ENABLED=0 // Disable CGO SET GOOS=linux // target platform is linux SET GOARCH=amd64 // The target processor architecture is amd64

If yourWindowsThe one used isPowerShellterminal, the syntax for setting environment variables is

$ENV:CGO_ENABLED=0
$ENV:GOOS="linux"
$ENV:GOARCH="amd64"

In yourWindowsAfter executing the above command in the terminal, execute the following command to get an executable file that can run on the Linux platform.

go build

Compile Mac executable files on Windows

Compile a 64-bit executable program for Mac platform under Windows:

Execute in cmd terminal:

SET CGO_ENABLED=0 SET GOOS=darwin SET GOARCH=amd64 go build

Execute in PowerShell terminal:

$ENV:CGO_ENABLED=0
$ENV:GOOS="darwin"
$ENV:GOARCH="amd64"
go build

Mac compiles Linux executable files

Compile on Mac to get a 64-bit executable program for Linux platform:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build

Compile Windows executable files on Mac

Compile on Mac to get a 64-bit executable program for Windows platform:

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

Compile Mac executable files on Linux

Compile a 64-bit executable program for Mac platform under Linux platform:

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build

Compile Windows executable files on Linux

Compile a 64-bit executable program for Windows platform under Linux platform:

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build

Now, start your Go language learning journey. Life is short, let's Go.

1/5 - (1 vote)

Leave a Reply

Your email address will not be published. Required fields are marked *