21-day Boutique blockchain free learning, in-depth expert led the way, to help developers easily play the blockchain! >>>
Golang How to install a third-party package:
1. Go get install
# 比如要安装 "github.com/gin-gonic/gin"$ go get github.com/gin-gonic/gin
- Note: Executing the go get command requires the GIT command to be installed and the GIT global variable to be configured.
2. Source Package Installation
Due to domestic network problems, many times the go get command does not install, so it is necessary to manually download the source package, and then copied to the $gopath/sr/directory
# 比如要安装"github.com/golang/protobuf/proto"# 去github.com/golang/protobuf下载源码包,# 拷贝到 $GOPATH/src/github.com/golang/protobuf$ cd $GOPATH/src/github.com/golang/protobuf$ go install
- Note: After the third package is installed, the application imports the package with the source code, not the. A file under the $GOPATH/pkg/. How to use pkg directly like the standard package FMT requires further research.
- Note: The above directly write $gopath, in Gopath only a directory when you can write this, press the TAB key will automatically switch to the actual directory, if the Gopath configuration of multiple directories to enter a specific directory.
Golang using third-party packages
How to use
Impor directly in the application
// 比如要使用 "github.com/gin-gonic/gin"import "github.com/gin-gonic/gin"
- Note: GO and Java are different: All import packages must be in the%gopath path, if directly import the package under the source path, the compilation will error. Because go will only find the dependency of the package under the $gopath, and will not be found under the current project.
Golang Adding environment variables
Most of the time we don't want our engineering code to be mixed with the downloaded third-party package code, and we want to build a directory ourselves, but we know that when we do go install it must be in the $GOPATH path, so we're going to add our own new directory to $gopath. For example, I built the src/directory under the/home/jerry/go/, which put the package I wrote Mymax/mymax.go
# 使用 vim 打开 ~/.bash 文件$ vim ~/.bash# 在里面添加刚才自己新建的工作目录export GOPATH=$GOPATH:/home/jerry/go/# 退出 vim 并让更改立即生效$ source ~/.bash# 安装自己的包, 发现不会报错了$ cd ~/go/src/myMax/$ go install
Installation of the Package management tool Govendor
According to the above method according to, after installation and then $gopath/bin/directory can see a Govendor file
$ ll ~/workspace/bin/-rwxrwxr-x 1 jerry jerry 13146288 Jul 23 04:44 govendor*# 将govendor 添加到环境变量$ vim ~/.bash# 在vim文件中添加export PATH=$PATH:/home/jerry/workspace/bin/$ source ~/.bash
Use of Govendor
In front of the configuration, we can now use the Govendor command in our own directory.
$ cd ~/go/src/myMax/# 生成vendor目录,生成vendor.jso$ govendor init$ cat vendor/vendor.json {"comment": "","ignore": "test","package": [],"rootPath": "myMax"}
Add a dependency package to the vendor directory
# govendor add +e 也可以$ govendor add +external
Understanding Golang Package Import
Https://www.cnblogs.com/sevenyuan/p/4548748.html
- When using a third-party package, the. A file is actually linked to a temporary directory compiled with the latest source code.
- The last element after import should be the path, which is the directory, not the package name.
- Fmt. The FMT in Println () is the package name.
- The M in Import m "Lib/math" is the package name.