Excellent software and practical tutorials
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 buildingGo language development environment.
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.
Install
Windows Installation
This installation example uses 64-bit Windows 10
System Installation Go1.14.1 executable version
For example.
Download the installation package selected in the previous step to your local computer.
Double-click the downloaded file and follow the steps below to install it.
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.gz
document:
wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz
Unzip the downloaded file to/usr/local
Under 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, addsudo
Run it again as root user. After execution, you can/usr/local/
See belowgo
Directory.
Configure environment variables: There are two files in Linux that can configure environment variables,/etc/profile
It is effective for all users;$HOME/.profile
It 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/profile
After the reboot takes effect, modify$HOME/.profile
Then use the source command to load$HOME/.profile
The 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/go
directory.
examine
After the previous installation process is completed, you can open the terminal window and entergo version
Command to view the installed Go version.
GOROOT and GOPATH
GOROOT
andGOPATH
are all environment variables, among whichGOROOT
is the path where we install the go development package. Starting from Go 1.8, the Go development package will be installed asGOPATH
Set 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 mod
mode to manage the dependent environment, and no longer force us to write code inGOPATH
The 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.io
orhttps://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 Code
andGoland
. VS Code
It is an open source editor from Microsoft, andGoland
It 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 Code
Full 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 Code
Official 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.Double-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 Box
Inputchinese
, select the first item in the result list, clickinstall
Install.
After the installation is complete, a prompt will appear in the lower right cornerRestart VS Code
After restarting, your VS Code will display Chinese!VSCode
Main interface introduction:
Install the Go extension
Now we have to install our VS Code editorGo
Extend the plug-in to support Go language development.
First Go Program
Hello World
Now let's create our first Go project -hello
. Create ahello
Table of contents.
go mod init
When creating a new project using the go module mode, we need to passgo mod init project name
The command is used to initialize the project. This command will be generated in the project root directory.go.mod
For example, we usehello
As the name of our first Go project, execute the following command.
go mod init hello
Writing Code
Next, create amain.go
document:
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 build
The 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 GOPATH
Find the one you want to compile in the src directoryhello
project
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.exe
Executable file.
You can execute this command directly in the terminal.hello.exe
document:
c:\desktop\hello>hello.exe Hello World!
We can also use-o
Parameter 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 Code
Currently usingPowerShell
As default terminal:It is highly recommended that you follow the steps below and select
cmd.exe
As the default terminal tool:At 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 suffix
cmd.exe
of that one and click the left mouse button.
at lastRestart the terminal that is already opened in VS CodeorRestart VS Code directlyThat's it.If the drop-down triangle does not appear, it does not matter. Press
Ctrl+Shift+P
A box will appear above VS Code. Enter the following information:shell
, then click the specified option to display the above interface.
go run
go run main.go
You can also execute programs, which essentially compiles first and then executes.
go install
go install
It means installation. It first compiles the source code to get the executable file, and then moves the executable file toGOPATH
In the bin directory. Because our environment variables are configuredGOPATH
The bin directory under , so we can execute the executable file directly from anywhere.
Cross-platform compilation
By default wego build
The 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 default
PowerShell
terminal.
If yourWindows
The 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 yourWindows
The one used isPowerShell
terminal, the syntax for setting environment variables is
$ENV:CGO_ENABLED=0
$ENV:GOOS="linux"
$ENV:GOARCH="amd64"
In yourWindows
After 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.