This article always follows the latest version, so outdated content will be deleted. If you find it out of date, please contact me if you can.
go mod
The following content has been integrated into Goland, so brothers who use Goland can write downgo mod tidy
Introduction to go modules
go modules is a new feature of golang 1.11, which is used to manage the dependencies of packages in modules .
How to use go mod
- Initialize the module
go mod init <项目模块名称>
- Dependency handling, according to the go.mod file
go mod tidy
- Copy the dependency package to the vendor directory under the project.
go mod vendor
If the package is blocked (wall), you can use this command and then use go build -mod=vendor to compile - show dependencies
go list -m all
- show detailed dependencies
go list -m -json all
- download dependent download
import
full path import
single line import
import "包名"
multiline import
It is best to arrange them in ASCII order. (My suggestion is to leave this matter to the IDE) Leave this mechanized matter to the machine. §(  ̄▽ ̄ )§
import (
"包名"
"包名"
"包名"
)
example
import (
_ "fmt"
"log"
"os"
)
Only import and not use
Indicates that only the initialization function ( go init function ) of the imported library is called and implemented, and the usage scenario is mostly a database connection.
import _ "包名"
example
import _ "fmt"
Rename imported packages
To prevent imported packages with the same name (Is it normal for different authors to have the same name?)(th _ th)
impor 新名 "包名"
example
import (
"crypto/rand"
mrand "math/rand" // 将名称替换为mrand避免冲突
)
Renaming of imported packages only affects the current source file. Other source files that import the same package can either use the original default name of the imported package or be renamed to a completely different name.
Do not display the name of the library, call the method directly
The effect is to remove the package name. At that time, it was not recommended. It is easy to have the same name in large projects. At this time, the package name is a key to distinguish functions.
import . "包名"
download
有些时候,你需要的包并没有被包含,因此你在写代码时不会出现代码补全;又或者你使用别人的项目发现一片红色,大概率就是你没有下载对应的包。
go get
go get 只用来下载普通的包,安装可执行程序,应该使用 go install
即 go get 现在仅仅只能增减依赖包
In Go 1.18,
go get
will no longer build packages; it will only be used to add, update, or remove dependencies ingo.mod
. Specifically,go get
will always act as if the-d
flag were enabled.
关于 url 可以到 https://pkg.go.dev/ 中直接查询
命令格式
go get url [附加参数]
- url :远程包的地址,如: https://github.com/davyxu/tabtoy
附加参数
使用 go get 时可以配合附加参数显示更多的信息及实现特殊的下载和安装操作,详见下表所示。
标记名称 | 描述 |
---|---|
-d | 让命令程序只执行下载动作,而不执行安装动作。 |
-f | 仅在使用-u标记时才有效。该标记会让命令程序忽略掉对已下载代码包的导入路径的检查。如果下载并安装的代码包所属的项目是你从别人那里Fork过来的,那么这样做就尤为重要了。 |
-fix | 让命令程序在下载代码包后先执行修正动作,而后再进行编译和安装。 |
-t | 让命令程序同时下载并安装指定的代码包中的测试源码文件中依赖的代码包。 |
-u | Let the command use the network to update existing code packages and their dependencies. By default, this command will only download code packages that do not exist locally from the network, and will not update existing code packages. |
go install
This command is used to compile and install the specified code packages and their dependent packages. When the dependent packages of the specified code package have not been compiled and installed, this command will process the dependent packages first. As with go buil
the d command, go install
code package arguments passed to the command should be provided as import paths.
Also, go build
most of the flags for commands can also be used for go install
commands. In fact, go install
the command only go build
does one more thing than the command, namely: install the compiled result file to the specified directory.