The first article from the public number: Go programming time
“Go programming time”, a can take you to learn the Go language column, at the same time welcome to search my namesake public account [Go programming time] (beautiful layout is more suitable for reading), the first time to get Go language dry materials.
1. Download and install the Go language
Download it at golang.google.cn/dl/
After downloading, directly double-click the MSI file for installation. I used to install the software in the Program Files directory under my E disk
Click Next until the following interface appears and the installation is complete.
2. Configure the Goland environment
Learning a programming language and using a good IDE can save you a lot of trouble.
I’m used to using PyCharm for Python projects because BEING used to JetBrains style IDES saves me a lot of the cost of getting familiar with new ides, so I’ll stick with JetBrains IDE for Go: Goland.
Goland download address: download.jetbrains.com/go/goland-2…
Double-click the downloaded EXE file. In addition to selecting the installation path, WHICH I used to change to disk E, select Next all the way until the following interface, select all according to your needs (it is recommended to select all)
Then go all the way to Next until the following interface appears. After installation is complete, select Run Gogland to Run immediately.
Goland is not available if you have not purchased the JetBrains activation code.
In order to make our learning more smooth, we will teach you how to get the use of Goland by cracking it.
First download the patches: c.biancheng.net/uploads/cou…
The downloaded ZIP package contains three files
- Jetbrains -agent.jar: Patch cracking
- Resources_cn. Jar: Chinese patch
- TXT: indicates the activation code
Copy jetBrains. Jar to your Goland installation directory in bin: E:\Program Files\JetBrains\ Goland 2019.2.3\bin
Then open the two files with an editor
Add the following line to the last line, and modify the following values according to your path
- javaagent: E: \ \ Program Files \ JetBrains \ GoLand 2019.2.3 \ bin \ JetBrains - agent. The jarCopy the code
Then go back to your Goland startup screen, click on Activation Code, copy the Activation code from Activation code.txt, fill it in, and click OK
Now that your Goland is ready to use, create my project directory and set up GOROOT.
After creating Project, click Files->Settings->GOPATH to add our Project directory F:\ go-player
Click on any go file and you will see the configuration entry at the arrow below. Click to enter and configure the GO runner.
Follow the instructions below to configure.
Remove parameter prompt
Set the goproxy
Set up goimports (automatic formatting plug-ins) to prompt you to click Yes to download the installation if you haven’t already installed it.
At this point, the environment is configured.
In the project root directory, create the following three folders, and create a hello.go file in the SRC directory.
Click the Run button, and in the console we see the familiar Hello, World!
3. Configure the VS Code environment
Set up user-level environment variables ahead of time
GOPATH = F:\Go-Player
PATH = %GOPATH%\bin # append
Copy the code
Someone asked in the comments yesterday, what are GOPATH and GOROOT? Why do I need to set this? Recall that when you learned Python, when you installed the Python interpreter, did you also set environment variables? It’s similar here.
GOROOT: In GO language represents the installation path of GO language compiler, tools, standard library, etc. It can tell the system where your go.exe is stored. If you do not set it, the system will not recognize it when you execute GO get and GO install.
The GOPATH environment variable represents Go’s working directory, which specifies where to look for Go packages, executables, and so on, and can be multiple directory representations. Here I set it to my workspace (directory is up to you) : F:\ go-player. If not, it defaults to the Go folder in your user directory.
At this point, the typical working directory structure for a GO project looks like this:
-
Bin directory: contains the executable program, note that it is executable, do not need to explain the execution.
-
PKG directory: Contains the packages or libraries to use.
-
SRC directory: This contains the go code source files, which are still organized by package.
So later I create the GO working directory, also according to this standard, first to explain.
Next, start configuring the VS Code environment.
Open your VS Code software, first confirm that the environment variables you set have taken effect, go to Terminal -> New Terminal, use CMD to view the environment variables.
As shown above, my environment variable is OK, if your output is pointing to your user directory: %USERPROFILE%\go I suggest you don’t bother (because no matter how many times I restart VS Code, its GOPATH will always point to %USERPROFILE%\ Go) and restart your computer directly.
After that, we will download two repositories from Github. The reason why we need to download them manually is because there are walls, and many plug-ins will fail to download online.
Create a directory SRC /goland.org/x/, go to the directory, and run the following command
$ git clone https://github.com/golang/tools.git
$ git clone https://github.com/golang/lint.git
Copy the code
Click file-Open Folder to install two plug-ins:
The first is an extension to the Go language
The second is Code Runner, which allows your VS Code to compile programs that run Go.
Click open a go file casually, and you will be prompted to install some tools in the lower right corner. Some of the installed packages cannot be downloaded due to the wall. In order to ensure a smooth download, you can set up the agent.
$ go env -w GOPROXY=https://goproxy.cn,direct
Copy the code
Then click Install All
Then you can see the installation progress in OUTPUT
The installed exe file will be placed under %GOPATH%/bin, i.e. F: go-player \bin
The SRC directory structure looks like this
At this point the environment is configured, you write HelloWorld, and run the view output, and you’re done.
4. Configure environment variables
When you use go env on a terminal, it prints out all environment variables associated with Go
$ go envset GO111MODULE= set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\wangbm\AppData\Local\go-build set GOENV=C:\Users\wangbm\AppData\Roaming\go\env set GOEXE=.exe set GOFLAGS= set GOHOSTARCH=amd64 set GOHOSTOS=windows set GONOPROXY= set GONOSUMDB= set GOOS=windows set GOPATH=E:\MING-Code\GoPlayer set GOPRIVATE= set GOPROXY = https://goproxy.cn, direct set GOROOT = D: \ \ Program Files \ (x86) the Go - 1.13.6 set set GOTMPDIR = GOSUMDB=sum.golang.org Set GOTOOLDIR=D:\Program Files (x86)\ go-1.13.6 \ PKG \tool\windows_amd64 set GCCGO= GCCGO set AR= AR set CC= GCC set CXX=g++ set CGO_ENABLED=1 set GOMOD= set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-configCopy the code
To see a few specific environment variables, add the go env
$ go env GOPATH
E:\MING-Code\GoPlayer
$ go env GOROOTD: \ \ Program Files \ Go - 1.13.6 (x86)$ go env GOPROXY
https://goproxy.cn,direct
Copy the code
There are many environment variables above, so it is sufficient to set only the following two
- The GO111MODULE is set to ON, indicating that the go Modules mode is used
$ go env -w GO111MODULE=on
Copy the code
- One is to enable the agent, in case the package fails to download (you may have set this earlier)
$ go env -w GOPROXY=https://goproxy.cn,direct
Copy the code