欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go語(yǔ)言中g(shù)o?mod?vendor使用方法

 更新時(shí)間:2022年10月25日 16:06:33   作者:test1280  
go mod vendor的功能是將新增的依賴包自動(dòng)寫(xiě)入當(dāng)前項(xiàng)目的 vendor目錄,下面這篇文章主要給大家介紹了關(guān)于Go語(yǔ)言中g(shù)o?mod?vendor使用的相關(guān)資料,需要的朋友可以參考下

1.背景

我們基于 go mod 機(jī)制來(lái)管理我們項(xiàng)目的依賴庫(kù)版本,其中 go.mod 記錄了依賴庫(kù)版本信息。

一般第三方依賴庫(kù)(包括公司內(nèi)網(wǎng)gitlab上的依賴庫(kù)),其源碼都不被包含在我們的項(xiàng)目?jī)?nèi)部,而是在編譯的時(shí)候go連接公網(wǎng)、內(nèi)網(wǎng)下載到本地GOPATH,然后編譯。

問(wèn)題是,有些時(shí)候需在無(wú)公網(wǎng)、無(wú)內(nèi)網(wǎng)(無(wú)法連接內(nèi)網(wǎng)gitlab)的情況下編譯go項(xiàng)目,如何做呢?

在此時(shí),需使用go mod vendor將項(xiàng)目的依賴庫(kù)下載到項(xiàng)目?jī)?nèi)部,作為項(xiàng)目的一部分來(lái)編譯。

PS:

  • 雖然通常不會(huì)也不需要在無(wú)公網(wǎng)、無(wú)內(nèi)網(wǎng)環(huán)境實(shí)時(shí)編譯,因?yàn)間o的可移植性很好,常以可執(zhí)行文件方式交付部署,但并不能排除此種可能;
  • 防止依賴庫(kù)因?yàn)槟撤N原因被刪除、移動(dòng),導(dǎo)致找不到依賴并編譯失敗;
  • 對(duì)新手來(lái)說(shuō),下載一些墻外的依賴可能略有困難;
  • 其他…

總之,我們的目的是使用 go mod vendor,將項(xiàng)目的依賴庫(kù)下載到項(xiàng)目?jī)?nèi)部,即項(xiàng)目中包含依賴庫(kù)源碼,依賴庫(kù)如同項(xiàng)目的一部分,也受到項(xiàng)目的版本管控(git、svn…)。

2.環(huán)境

go環(huán)境:

D:\workspace\demo>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\梁翠翠\AppData\Local\go-build
set GOENV=C:\Users\梁翠翠\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\gopath\pkg\mod
set GONOPROXY=gitlab.ebupt.com
set GONOSUMDB=gitlab.ebupt.com
set GOOS=windows
set GOPATH=C:\gopath
set GOPRIVATE=gitlab.ebupt.com
set GOPROXY=https://goproxy.io
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.2
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\workspace\demo\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\梁翠翠\AppData\Local\Temp\go-build648873300=/tmp/go-build -gno-record-gcc-switches

goland版本:

3.使用

示例:

項(xiàng)目demo由兩個(gè)文件構(gòu)成:

1.main.go:項(xiàng)目依賴gopkg.in/yaml.v2 module(版本:v2.4.0);

2.go.mod:記錄當(dāng)前項(xiàng)目demo依賴yaml module;

最常用、最簡(jiǎn)單的辦法是,直接執(zhí)行g(shù)o mod vendor:

執(zhí)行g(shù)o mod vendor,將此項(xiàng)目依賴的gopkg.in/yaml.v2@v2.4.0下載到項(xiàng)目demo的根目錄vendor中,并按照特定格式、規(guī)范組織。

如果此時(shí)你ctrl+鼠標(biāo)點(diǎn)擊import后面的yaml.v2時(shí),將自動(dòng)跳轉(zhuǎn)到vendor目錄下的yaml.v2:

而不再是GOPATH中的yaml.v2:

goland在提示你,當(dāng)前項(xiàng)目使用的是項(xiàng)目demo中vendor目錄下得yaml.v2,而非GOPATH中的yaml.v2。

即使此刻,我們將GOPATH中的yaml.v2刪除:

在項(xiàng)目中直接編譯demo,不再需要下載yaml.v2依賴:

4.原理

官方文檔請(qǐng)參見(jiàn)【重要!??!】:

1.https://golang.org/ref/mod#go-mod-vendor

2.https://golang.org/ref/mod#vendoring

命令行幫助:

D:\workspace\demo>go help mod vendor
usage: go mod vendor [-e] [-v]

Vendor resets the main module's vendor directory to include all packages
needed to build and test all the main module's packages.
It does not include test code for vendored packages.

The -v flag causes vendor to print the names of vendored
modules and packages to standard error.

The -e flag causes vendor to attempt to proceed despite errors
encountered while loading packages.

See https://golang.org/ref/mod#go-mod-vendor for more about 'go mod vendor'.

關(guān)鍵部分:

1.The go mod vendor command constructs a directory named vendor in the main module’s root directory that contains copies of all packages needed to support builds and tests of packages in the main module.

2.When vendoring is enabled, the go command will load packages from the vendor directory instead of downloading modules from their sources into the module cache and using packages those downloaded copies.

3.If go.mod changed since vendor/modules.txt was generated, go mod vendor should be run again.

如果 go.mod 發(fā)生變化,應(yīng)當(dāng)重新執(zhí)行 go mod vendor!

4.Note that go mod vendor removes the vendor directory if it exists before re-constructing it. Local changes should not be made to vendored packages. The go command does not check that packages in the vendor directory have not been modified, but one can verify the integrity of the vendor directory by running go mod vendor and checking that no changes were made.

  1. 執(zhí)行g(shù)o mod vendor將刪除項(xiàng)目中已存在的vendor目錄;
  2. 永遠(yuǎn)不要對(duì)vendor中的依賴庫(kù)進(jìn)行二次修改、更改!
  3. go命令不檢查vendor中的依賴庫(kù)是否被修改;

5.If the vendor directory is present in the main module’s root directory, it will be used automatically if the go version in the main module’s go.mod file is 1.14 or higher. To explicitly enable vendoring, invoke the go command with the flag -mod=vendor. To disable vendoring, use the flag -mod=readonly or -mod=mod.

在go version >= 1.14時(shí),如果存在vendor目錄,將自動(dòng)啟用vendor。

-mod=vendor
-mod=readonly
-mod=mod

6.When vendoring is enabled, build commands like go build and go test load packages from the vendor directory instead of accessing the network or the local module cache.

5.參考

  • https://golang.org/ref/mod#go-mod-vendor
  • https://golang.org/ref/mod#vendoring
  • https://yanbin.blog/go-use-go-mod-manage-dependencies/
  • https://cloud.tencent.com/developer/article/1626849
  • https://cloud.tencent.com/developer/article/1604866

到此這篇關(guān)于Go語(yǔ)言中g(shù)o mod vendor使用方法的文章就介紹到這了,更多相關(guān)go mod vendor使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Golang中的godoc使用簡(jiǎn)介(推薦)

    Golang中的godoc使用簡(jiǎn)介(推薦)

    Godoc是go語(yǔ)言的文檔化工具,類似于文檔化工具godoc,類似于Python的Docstring和Java的Javadoc,這篇文章主要介紹了Golang中的godoc使用簡(jiǎn)介,需要的朋友可以參考下
    2022-10-10
  • Go語(yǔ)言讀寫(xiě)鎖RWMutex的源碼分析

    Go語(yǔ)言讀寫(xiě)鎖RWMutex的源碼分析

    本篇文章我們將一起來(lái)學(xué)習(xí)下Go語(yǔ)言中的讀寫(xiě)鎖sync.RWMutex。文中的示例講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-10-10
  • golang?pprof?監(jiān)控goroutine?thread統(tǒng)計(jì)原理詳解

    golang?pprof?監(jiān)控goroutine?thread統(tǒng)計(jì)原理詳解

    這篇文章主要為大家介紹了golang?pprof?監(jiān)控goroutine?thread統(tǒng)計(jì)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • go語(yǔ)言的panic和recover函數(shù)用法實(shí)例

    go語(yǔ)言的panic和recover函數(shù)用法實(shí)例

    今天小編就為大家分享一篇關(guān)于go語(yǔ)言的panic和recover函數(shù)用法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • golang 一次性定時(shí)器Timer用法及實(shí)現(xiàn)原理詳解

    golang 一次性定時(shí)器Timer用法及實(shí)現(xiàn)原理詳解

    這篇文章主要為大家介紹了golang 一次性定時(shí)器Timer用法及實(shí)現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Golang學(xué)習(xí)之map的用法詳解

    Golang學(xué)習(xí)之map的用法詳解

    在Golang(又稱Go語(yǔ)言)中,map是一種非常有用的數(shù)據(jù)結(jié)構(gòu),所以這篇文章小編就來(lái)帶大家一起深入了解一下map的用法,感興趣的小伙伴可以了解一下
    2023-06-06
  • 8種超簡(jiǎn)單的Golang生成隨機(jī)字符串方式分享

    8種超簡(jiǎn)單的Golang生成隨機(jī)字符串方式分享

    這篇文章主要為大家詳細(xì)介紹了8種超簡(jiǎn)單的Golang生成隨機(jī)字符串方式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • Go實(shí)現(xiàn)凱撒密碼加密解密

    Go實(shí)現(xiàn)凱撒密碼加密解密

    這篇文章主要為大家介紹了Go實(shí)現(xiàn)凱撒密碼加密解密示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • GOLANG使用Context實(shí)現(xiàn)傳值、超時(shí)和取消的方法

    GOLANG使用Context實(shí)現(xiàn)傳值、超時(shí)和取消的方法

    這篇文章主要介紹了GOLANG使用Context實(shí)現(xiàn)傳值、超時(shí)和取消的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • GoLang unsafe包詳細(xì)講解

    GoLang unsafe包詳細(xì)講解

    從golang的定義來(lái)看,unsafe 是類型安全的操作。顧名思義,它應(yīng)該非常謹(jǐn)慎地使用; unsafe可能很危險(xiǎn),但也可能非常有用。例如,當(dāng)使用系統(tǒng)調(diào)用和Go結(jié)構(gòu)必須具有與C結(jié)構(gòu)相同的內(nèi)存布局時(shí),您可能別無(wú)選擇,只能使用unsafe
    2022-10-10

最新評(píng)論