手把手教你怎麼導入Go語言第三方庫
一、使用 go get擷取
GO的指令
go get
讓我們可以友善快捷的從網絡中下載下傳或更新Go語言包及其依賴檔案,并将他們編譯和安裝。
- 先在指令行模式下輸入
,可檢視以下資訊。go --help
Go is a tool for managing Go source code.
Usage:
go [arguments]
The commands are:
bug start a bug report
build compile packages and dependencies
clean remove object files and cached files
doc show documentation for package or symbol
env print Go environment information
fix update packages to use new APIs
fmt gofmt (reformat) package sources
generate generate Go files by processing source
get add dependencies to current module and install them
install compile and install packages and dependencies
list list packages or modules
mod module maintenance
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet report likely mistakes in packages
Use "go help " for more information about a command.
Additional help topics:
buildconstraint build constraints
buildmode build modes
c calling between Go and C
cache build and test caching
environment environment variables
filetype file types
go.mod the go.mod file
gopath GOPATH environment variable
gopath-get legacy GOPATH go get
goproxy module proxy protocol
importpath import path syntax
modules modules, module versions, and more
module-get module-aware go get
module-auth module authentication using go.sum
module-private module configuration for non-public modules
packages package lists and patterns
testflag testing flags
testfunc testing functions
Use "go help " for more information about that topic.
複制
我們可以看到
get add dependencies to current module and install them
這就是我們今天要用到的東西。
然後檢視它的幫助資訊:
go get --help
usage: go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages]
Run 'go help get' for details.
複制
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAjM2EzLcd3LcJzLcJzdllmVldWYtl2Pn5GcuQzMyEjZkRmZzUTNlV2YwQWZmNTNmBDMyYmZxMDZlJmZvw1MzgTMzMjMtUGall3LcVmdhNXLwRHdo9CXt92YucWbpRWdvx2Yx5yazF2Lc9CX6MHc0RHaiojIsJye.png)
找到我們要擷取的庫。
複制連結
https://github.com/go-sql-driver/mysql
使用
go get github.com/go-sql-driver/mysql
結果報錯:
go: missing Git command. See https://golang.org/s/gogetcmd
package github.com/go-sql-driver/mysql: exec: "git": executable file not found in %PATH%
複制
這裡要安裝Git,部落客是安裝了Git的,不過這裡需要配置一下。
在系統變量的PATH裡面添加Git的bin目錄的路徑。
然後再運作
go get github.com/go-sql-driver/mysql
現在就成功了。
這個
github.com/go-sql-driver/mysql
被稱為遠端導入路徑,其實go get被稱為遠端動态導入GO包。其實
go get
幹的事就是從分布式版本控制系統的倉庫中找出代碼包并将它編譯以及安裝。
倉庫名 | VCS | VCS | VCS |
---|---|---|---|
BitBucket | Mercurial | Git | |
GitHub | Git | ||
Google Code Project Hosting | Git | Mercurial | Subversion |
Launchpad | Bazaar |
一般情況下,代碼包遠端導入路徑中的第一個元素就是代碼托管網站的主域名。在靜态分析的時候,
go get
指令會将代碼包遠端導入路徑與預置的代碼托管站點的主域名進行比對。如果比對成功,則在對代碼包遠端導入路徑的初步檢查後傳回正常的傳回值或錯誤資訊。如果比對不成功,則會再對代碼包遠端導入路徑進行動态分析。
二、直接下載下傳GO庫
在 github 或者其他地方下載下傳Go庫。
然後在系統變量檢視GOPATH。
然後打開GOPATH的src目錄
将檔案複制進去。
然後就成功擷取了。
三、參數介紹
usage: go get [-d] [-f] [-t] [-u] [-v] [-fix] [-insecure] [build flags] [packages]
Run 'go help get' for details.
複制
然後我們可以看一看
go help get
裡有關參數的介紹。
The -d flag instructs get to stop after downloading the packages; that is,
it instructs get not to install the packages.
The -f flag, valid only when -u is set, forces get -u not to verify that
each package has been checked out from the source control repository
implied by its import path. This can be useful if the source is a local fork
of the original.
The -fix flag instructs get to run the fix tool on the downloaded packages
before resolving dependencies or building the code.
The -insecure flag permits fetching from repositories and resolving
custom domains using insecure schemes such as HTTP. Use with caution.
The -t flag instructs get to also download the packages required to build
the tests for the specified packages.
The -u flag instructs get to use the network to update the named packages
and their dependencies. By default, get uses the network to check out
missing packages but does not use it to look for updates to existing packages.
The -v flag enables verbose progress and debug output.
複制
簡單來說就是:
- -d 下載下傳完成後就停止工作,不安裝庫
- -f 這個參數隻有在使用了-u 參數時才有用,強制-u不去驗證import的每一個包是否已經擷取了,這對本地fork的包非常有用。
- -fix 這個-fix參數表示在解決依賴關系或建構代碼之前先運作fix工具。
- -insecure 該參數允許通過不安全(例如 HTTP)的自定義域擷取并解析使用存儲庫。
- -t 該參數允許在下載下傳該包時也下載下傳測試該包需要的包。
-
-u 參數允許 get 使用網絡來更新包
以及它們的依賴。預設情況下,get 使用網絡檢查丢失的包,但不使用它來查找現有包的更新。
- -v 啟用詳細進度和調試資訊輸出。