1. Common Environment Variables
1.1 GOROOT
$GOROOT is the local directory where the Go package is installed.
| |
1.2 GOPATH
$GOPATH is the Go workspace directory, used to hold code, third-party libraries, compiled intermediate files, and so on.
If you need to configure multiple directories, on Mac and Linux you can separate them with :, and on Windows you can use ;.
Note that packages fetched by go get are stored in the first directory by default.
Under the $GOPATH directory, there are conventionally three subdirectories:
- src: holds source code
- pkg: holds the intermediate files generated during compilation
- bin: holds the executable files produced by compilation. Usually you add
;%GOPATH%\binto the PATH environment variable so that the compiled programs are easy to run.
In project development, there are two kinds of code to manage:
- Third-party libraries
- Business project source code
There are three ways to manage GOPATH:
- Multiple projects sharing a single global GOPATH. Simple, but with insufficient project isolation.
- Third-party libraries and business source code placed separately, configuring two GOPATHs.
- Each project using its own separate GOPATH, with a script to compile it.
Usually, for simplicity, multiple projects share one GOPATH.
| |
/c/data/go/ is the GOPATH directory I configured.
In the src directory there are both third-party libraries such as github\gitlab and business-related project code.
1.3 GOBIN
GOBIN is the installation directory for executables after compilation. If GOBIN is set, the compiled executables will not be installed into the bin directory under GOPATH. If GOPATH contains multiple directories, then GOBIN must be set.
2. Dependency Management
Under the GOPATH path there is not only project code but also a large number of third-party dependency libraries. When there are many projects and dependencies, the amount of code under the GOPATH path becomes very large and hard to manage.
2.1 vendor
Starting with 1.5, Go added the vendor mechanism. The vendor mechanism is simply adding a vendor folder to a project to hold dependencies and isolate the project.
When using go run or go build, Go first looks for dependencies in the vendor folder under the current path, and if not found there, queries GOPATH. But when using the go get or go install commands, Go still installs dependencies into GOPATH.
2.2 dep
Installation command:
| |
Basic commands:
| |
The help documentation already describes how to use dep quite clearly. In addition, godep and go modules can also be used to manage dependency packages.
3. Basic Commands
- go env
View all of go’s configuration information.
| |
- go get
Download or update the specified dependency package, then compile and install it.
| |
- go build
Compile source files and dependency packages.
- go install
Compile and install the specified dependency package.
- go clean
Remove files and directories produced by other commands.
- go run
Compile and run a source file.
4. Hello World
Download and install
| |
Create the file hello.go
| |
Compile and run
| |
Open a browser and visit http://localhost:8080

