Table of contents
Install and download GO language
VSCode switches the default terminal
This article is based on Go1.20.1 version, starting from scratch, leading you to download and build the Go language and development environment.
Note : After version 1.14 of the Go language, it is recommended to use go modules to manage dependencies, and it is no longer necessary to write the code in the GOPATH directory.
Install and download GO language
download
download link
Go official website download address: https://golang.org/dl/
Go official mirror site (recommended): All releases - The Go Programming Language
version selection
It is recommended to download the executable file version for Windows and Mac platforms, and download the compressed file version for Linux platforms.
Note: Since the update iteration speed of the Go language is fast, you can choose the latest version to download here, but the installation process is roughly the same.
Install
Windows installation
This installation example takes 64位Win10
system installation Go1.20.1可执行文件版本
as an example.
Download the installation package selected in the previous step to the local.
Double-click the downloaded file, and then follow the steps in the figure below to install it.
Specify a Go installation directory, it is recommended not to place the C drive.
Click Install.
Wait for the program to finish installing, then click "Finish" to exit the installer.
Install under Linux
If you don’t want to type 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 the cross-platform compilation at the end of the article for details) and then copied to the Linux server. It is running, which is also the advantage of easy deployment of go programs across platforms.
We select and download go1.20.1.linux-amd64.tar.gz
the file on the version selection page:
wget https://dl.google.com/go/go1.14.1.linux-amd64.tar.gz
Unzip the downloaded file to /usr/local
a directory:
tar -zxvf go1.14.1.linux-amd64.tar.gz -C /usr/local # 解压
If it prompts that there is no permission, sudo
run it again as the root user. After execution, you /usr/local/
can see go
the directory below.
Configure environment variables: There are two files under Linux to configure environment variables, which /etc/profile
are valid for all users and $HOME/.profile
valid for the current user. Choose 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
After modification /etc/profile
, restart to take effect. $HOME/.profile
After modification, use the source command to load $HOME/.profile
the file to take effect. examine:
~ go version
go version go1.14.1 linux/amd64
Install under Mac
Download the executable file version, just click Next to install, and go will be installed /usr/local/go
in the directory by default.
examine
After the installation process in the previous step is completed, you can open a terminal window and enter go version
commands to view the installed Go version.
GOROOT and GOPATH
GOROOT
and GOPATH
are both environment variables, which GOROOT
is the path where we install the go development package, and starting from the Go 1.8 version, the Go development package will set a default directory for after the installation is complete GOPATH
, and Go is enabled in Go1.14 and later versions After the Module mode, it is not necessary to write the code to the GOPATH directory, so we don’t need to configure GOPATH by ourselves , just use the default one.
To view the path on your computer GOPATH
, just open the terminal and enter the following command and press Enter:
go env
GOPATH
Find the corresponding specific path in the content output by the terminal .
GOPROXY
After Go1.14, it is recommended to use go mod
patterns to manage the dependent environment, and it is no longer mandatory for us to write the code in the GOPATH
src directory below. You can write go code anywhere on your computer. (Some tutorials on the Internet are applicable before version 1.11.)
The default GoPROXY configuration is: GOPROXY=https://proxy.golang.org,direct
, because the domestic access is not available https://proxy.golang.org
, so we need to change a PROXY, it is recommended to use https://goproxy.io
or https://goproxy.cn
.
You can execute the following command to modify GOPROXY:
go env -w GOPROXY=https://goproxy.cn,direct
Go development editor
Theoretically, any text editor can be used for Go language development, and it is recommended to use VS Code
and Goland
. In this article we useVS Code进行开发。
Introduction to VS Code
VS Code
The full name is a free modern lightweight code editor open sourced by Microsoft, which supports syntax highlighting, smart code completion, custom hotkeys, bracket matching, code snippets, code comparison Diff, and more in almost all mainstream development languages Visual Studio Code
. Features such as GIT, support plug-in expansion, support Win, Mac and Linux platforms.
Although not as powerful as some IDEs, it is sufficient for our daily Go development after adding the Go extension plug-in.
download and install
VS Code
Official download link: Download Visual Studio Code - Mac, Linux, Windows
All three mainstream platforms are supported, please choose the corresponding installation package according to your computer platform.
Double-click the downloaded installation file, and double-click to install.
configuration
Install the Simplified Chinese plugin
Click the last item in the left menu bar 管理扩展
, 搜索框
enter C in hinese
, select the first item in the result list, and click install
Install.
After the installation is complete, there will be a prompt in the lower right corner 重启VS Code
. After restarting, your VS Code will display Chinese.
Go extensions
Go
Now we need to install extensions for our VS Code editor to support Go language development.
Just install it directly.
first Go program
Hello World
Now let's create our first Go project -- hello
.
go mod init
When using the go module mode to create a new project, we need togo mod init 项目名
initialize the project through the command, which will generate go.mod
files in the project root directory. For example, we use hello
the name as our first Go project and execute the following command.
go mod init hello
Note : If a prompt pops up in the lower right corner of VS Code asking you to install the plug-in, be sure to click install all to install it.
write
Next create a file in that directory main.go
:
package main // 声明 main 包,表明当前是一个可执行程序
import "fmt" // 导入内置 fmt 包
func main(){ // main函数,是程序执行的入口
fmt.Println("Hello World!") // 在终端打印 Hello World!
}
Note : The fmt package can be imported automatically
compile
go build
command means to compile the source code into an executable file.
(The way to enter the terminal is to click the exclamation mark in the lower left corner)
Execute in the hello directory:
go build
The compiled executable file will be saved in the current directory where the compilation command is executed. If it is a platform, the executable file Windows
will be found in the current directory .hello.exe
This file can be executed directly in the terminal hello.exe
:
c:\desktop\hello>hello.exe
Hello World!
Note : Enter hello.exe error here, please see below
We can also use -o
parameters to specify the name of the compiled executable file.
go build -o heiheihei.exe
VSCode switches the default terminal
Friends who just made a mistake can try:
./hello.exe
You will find that it can be successfully run in this way. Why?
In fact, our terminal is different. The terminal used at the beginning is powershell, but in the article we use cmd.
Then let's change its default terminal to cmd!
After clicking to enter:
Select Command Prompt.
Finally, restart the opened terminal in VS Code or restart VS Code directly .
(Finally, the article was written by the author after studying Li Wenzhou's blog, so there will be similarities. Here is also the blog address of the big brother Li Wenzhou: here if you are interested, you can take a look )