【Go】Go language development environment installation
Import
Installation environment: Winowds
I am installing win7 now, and the overall steps are the same as win10, but there are some differences when some parts are displayed, which does not affect;
【noun】
Compiler: first compile the code into an executable file, and then execute it; – full text translation
Applicable languages:
- C
- C++
- Java
- Go
Interpreter: It can be executed directly without compiling the code; –real-time translation
Applicable languages:
- Python
- PHP
- JavaScript
- Ruby
Resources
Official website: https://golang.google.cn/dl/ Forum: https://studygolang.com/
Website homepage
Windows installation package
The latest version: go1.20.2
Install
Install
The installation is complete
View installation files
Test and verify the switching path to the bin folder:
Execute command: go version
log:
D:\software_install\Go\bin>go version
go version go1.20.2 windows/amd64
Configure environment variables
**Purpose: **It is convenient to run the go compiler later, without manually specifying the installation location of go;
Method:
Computer-Properties-Advanced System Settings-Environment Variables. When I came in, I found that it has been configured. It may be that the current version installs built-in functions to realize the configuration of environment variables, so we don’t need to make drastic operations.
Just switch all the way through, try to see if the environment variable configuration is correct, and perform a simple operation.
Check the installed version
C:\Users\ws>go version
go version go1.20.2 windows/amd64
Check the environment
Check the go environment
go env
E:\GoProject\src\utils>go env
set GO111MODULE=auto
set GOARCH=amd64
set GOBIN=E:\GoProject\bin
set GOCACHE=C:\Users\ws\AppData\Local\go-build
set GOENV=C:\Users\ws\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\GoProject\pkg\mod
set GONOPROXY=
Other configuration
Create a directory
The role of this directory: All project resources and source codes about go are stored in this location in the later stage, which is convenient for code management;
Next, make a finer division and create three folders bin
, pkg
, src
;
E:\GoProject -bin -pkg - src explain: bin : Path to the exe file generated by executing `go install` pkg : The package file generated by executing `go install` will be stored in the pkg folder [introduction below]
src
: all projects and their source code are stored here, the first project: asset management system, the second one: operation system
-asset manage.go - business opr.go
Environment variable configuration
GOBIN
GOPATH
GOROOT
Briefly explain the above environment variables:
GOBIN : Save the path of the exe file generated by go install
and the path of the executable file in the use case;
GOPATH: used to specify the address and working path of the development project;
GOROOT: Used to specify the installation path of go.
Code test
Write
Create a new project under E:\GoProject\src, named first
, and then create a file app.go
under the project:
package main import "fmt" func main(){<!-- --> fmt.Println("hello go") }
Run
Switch to the path where app.go
is located:
method 1:
command to run
Execute the command go run app.go
to run the project:
E:\GoProject\src\first>go run app.go hello go
Method 2:
Compile and run
Excuting an order:
go build
There are some small episodes when running:
E:\GoProject\src\first>go build
go: go.mod file not found in current directory or any parent directory; see ‘go help modules’
Next, we will build bridges across mountains and rivers, and when we encounter problems, we will bravely face them and solve them;
Cause Analysis:
Go environment configuration problem. Related to golang’s package management.
solution:
If you are a Windows system, press the shortcut key “Win + R”, enter cmd, and open the terminal. enter:
go env -w GO111MODULE=auto
explain:
GO111MODULE is a new version of module management introduced by Go 1.11. It is officially supported in version 1.12. It is an official package management solution provided by the Go language.
The GO111MODULE environment variable is used to enable or disable module support in the Go language. It has three optional values: off, on, and auto, and the default is auto.
-
GO111MODULE=off
Without module support, the go command line will not support the module function, and the way to find dependent packages will follow the old version’s way of searching through the vendor directory or GOPATH mode. -
GO111MODULE=on
Module support, the go command line will use modules, and will not search in the GOPATH directory at all. It can also be explained that go ignores the $GOPATH folder and only downloads dependencies according to go.mod. -
GO111MODULE=auto
By default, the go command line will determine whether to enable the module function based on the current directory. This situation can be divided into two situations:
(1) The current directory is outside GOPATH/src and the directory contains the go.mod file, enable module support.
(2) The current file is under the directory containing the go.mod file.When there is a go.mod file in the outer layer of $GOPATH/src and the root directory, module support is enabled; otherwise, there is no module support.
How to use Go mod
//Initialization module: Go mod init <project module name> //Dependency processing, according to the go.mod file Go mod tidy //Copy the dependency package to the vendor directory of the project Go mod vendor // show dependencies Go list -m all //show detailed dependencies Go list -m -json all //Download dependencies Go mod download [path@version]
Then continue: go build
E:\GoProject\src\first>go build
E:\GoProject\src\first>
There is no error reported this time, let’s see what happened after we executed this command
An executable file first.exe
named after the project name is generated in the E:\GoProject\src\first
project directory, click to run it If there is any change, I found that the terminal flashed for a while and then quickly exited. This way does not work. Let’s go another way first, switch to the path where the first.exe
file is located in the terminal, and execute as follows:
E:\GoProject\src\first>first.exe hello go
This time it is very clear that the result of the console output is the data we want to see;
Supplement:
The reason why double-clicking the exe file to flash just now:
1. The main reason is that the command execution is too fast. After the execution is over, the life cycle is also over, and the terminal will naturally exit; this problem can be avoided by using the cmd command terminal. If you want to double-click the exe file to run, you need to add a little Wait for a while, the code can be changed like this:
package main import "fmt" import "time" func main(){<!-- --> fmt.Println("hello go") time. Sleep(5 * time. Second) }
build specified name
The command used just now is the name of the exe file generated by go build
is the name of the project. If you need to specify the name, add a parameter -o xxx.exe
E:\GoProject\src\first>go build -o aaa E:\GoProject\src\first>aaa 'aaa' is not recognized as an internal or external command, operable program or a batch file. E:\GoProject\src\first>go build -o aaa.exe E:\GoProject\src\first>aaa.exe hello go E:\GoProject\src\first>
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-WoIpNKsD-1678612696572)(【Go】Go language development environment installation.assets/1678608302838.png)]
The above two operating modes of mode 1 and mode 2 are essentially the same;
Method 3:
Excuting an order:
go install
First.exe is generated in the bin folder. At this time, it is just time to introduce why we built this folder and configure the environment variables. The purpose is for this, the exe generated when go install
The files are stored in the bin
folder;
pkg
1. Create a new file tools.go
in E:\GoProject\src\utils
package utils func add(n1 int,n2 int)(ret int){<!-- --> return n1 + n2 }
2. Execute cmd: go install
3. The utils.a file will be generated in E:\GoProject\pkg\windows_amd64
The difference between go build and go install
Both go build and go install commands can generate executable files, understand their small differences to choose the one that suits you.
go install and go build run without parameters, both will be compiled in the current directory, if the current directory is the software main program (package main)
go build will generate an executable file in the current directory
go install will put it in $GOPATH/bin.
go install will also compile the imported dependency package to $GOPATH/pkg and cache it. If the package has not been changed, the cache will be used directly for the next compilation. The go build command plus parameter -i can also achieve the effect of go install.
If the current directory is a non-main package, go install will directly install the compilation result to $GOPATH/pkg.
If the project is very large and it takes a long time to compile each time, it is recommended to use go install to compile and make full use of its caching function for dependent packages
Summary
1. Load the go compiler;
2. Installation;
3. In the installation directory is all the content related to the go compiler.
Under the installation command, there is a go executable file in the bin directory, based on which the compiler is started.
- You can directly find the path to find it, and then run it, which is a bit troublesome.
- Add the executable file of go to the environment variable, and open the command terminal at any location to run smoothly. (This is also an environment variable configuration operation that most development languages need to do. Once the configuration is permanently effective, the premise is not to reinstall the system, such as in java, python, node)
Based on the above settings, although the development of go can be realized,
If you want to use the go compiler more conveniently, you need to make further settings:
-
create command
Used to store code, compiled executable files, and compiled package files.
xxx -bin -pkg - src
-
environment variable
GOBIN : Save the path of the executable file generated after compilation; GOPATH: Used to specify the address of the development project, the compiled executable file, package file, and working path; GOROOT: Used to specify the installation path of go.
write code, then run
Wrote two projects:
- Generate an executable file after first compilation
- utils generates a package file after compilation
run project
- go run xxx.go : Run the project code, internally compile and put the compiled file in the temporary directory of the system, and then execute it automatically;
- go build: Run the project code, manually compile and generate an executable file, and then run it automatically.
- go install: Generate executable files + package files, and put the compiled files in the bin or pkg directory.