Summary of the use of Golang third Party Package

Source: Internet
Author: User

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

    1. When using a third-party package, the. A file is actually linked to a temporary directory compiled with the latest source code.
    2. The last element after import should be the path, which is the directory, not the package name.
    3. Fmt. The FMT in Println () is the package name.
    4. The M in Import m "Lib/math" is the package name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.