最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

1.背景

我們基于 go mod 機制來管理我們項目的依賴庫版本,其中 go.mod 記錄了依賴庫版本信息。

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

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

在此時,需使用go mod vendor將項目的依賴庫下載到項目內(nèi)部,作為項目的一部分來編譯。

PS:

  • 雖然通常不會也不需要在無公網(wǎng)、無內(nèi)網(wǎng)環(huán)境實時編譯,因為go的可移植性很好,常以可執(zhí)行文件方式交付部署,但并不能排除此種可能;
  • 防止依賴庫因為某種原因被刪除、移動,導致找不到依賴并編譯失?。?/li>
  • 對新手來說,下載一些墻外的依賴可能略有困難;
  • 其他…

總之,我們的目的是使用 go mod vendor,將項目的依賴庫下載到項目內(nèi)部,即項目中包含依賴庫源碼,依賴庫如同項目的一部分,也受到項目的版本管控(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.使用

示例:

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

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

2.go.mod:記錄當前項目demo依賴yaml module;

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

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

如果此時你ctrl+鼠標點擊import后面的yaml.v2時,將自動跳轉(zhuǎn)到vendor目錄下的yaml.v2:

而不再是GOPATH中的yaml.v2:

goland在提示你,當前項目使用的是項目demo中vendor目錄下得yaml.v2,而非GOPATH中的yaml.v2。

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

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

4.原理

官方文檔請參見【重要?。?!】:

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ā)生變化,應當重新執(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將刪除項目中已存在的vendor目錄;
  2. 永遠不要對vendor中的依賴庫進行二次修改、更改!
  3. go命令不檢查vendor中的依賴庫是否被修改;

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時,如果存在vendor目錄,將自動啟用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語言中g(shù)o mod vendor使用方法的文章就介紹到這了,更多相關(guān)go mod vendor使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Golang交叉編譯之跨平臺編譯使用詳解

    Golang交叉編譯之跨平臺編譯使用詳解

    這篇文章主要為大家介紹了Golang交叉編譯之跨平臺編譯使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • golang基于errgroup實現(xiàn)并發(fā)調(diào)用的方法

    golang基于errgroup實現(xiàn)并發(fā)調(diào)用的方法

    這篇文章主要介紹了golang基于errgroup實現(xiàn)并發(fā)調(diào)用,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • 深入淺析Go中三個點(...)用法

    深入淺析Go中三個點(...)用法

    這篇文章主要介紹了深入淺析Go中三個點(...)用法,需要的朋友可以參考下
    2021-10-10
  • golang將多路復異步io轉(zhuǎn)成阻塞io的方法詳解

    golang將多路復異步io轉(zhuǎn)成阻塞io的方法詳解

    常見的IO模型有阻塞、非阻塞、IO多路復用,異,下面這篇文章主要給大家介紹了關(guān)于golang將多路復異步io轉(zhuǎn)成阻塞io的方法,文中給出了詳細的示例代碼,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-09-09
  • Go語言變量與基礎(chǔ)數(shù)據(jù)類型詳情

    Go語言變量與基礎(chǔ)數(shù)據(jù)類型詳情

    Go 是靜態(tài)(編譯型)語言,是區(qū)別于解釋型語言的弱類型語言(靜態(tài):類型固定,強類型:不同類型不允許直接運算),下面文章將對其進行詳細介紹,需要的朋友可以參考一下
    2021-09-09
  • 使用Go語言實現(xiàn)跨域資源共享(CORS)設(shè)置

    使用Go語言實現(xiàn)跨域資源共享(CORS)設(shè)置

    在Web開發(fā)中,跨域資源共享(CORS)是一種重要的安全機制,它允許許多資源在一個網(wǎng)頁上被另一個來源的網(wǎng)頁所訪問,然而,出于安全考慮,瀏覽器默認禁止這種跨域訪問,為了解決這個問題,我們可以使用Go語言來設(shè)置CORS,需要的朋友可以參考下
    2024-06-06
  • Go strconv包實現(xiàn)字符串和基本數(shù)據(jù)類型轉(zhuǎn)換的實例詳解

    Go strconv包實現(xiàn)字符串和基本數(shù)據(jù)類型轉(zhuǎn)換的實例詳解

    在Go語言(Golang)的編程實踐中,strconv包是一個非常重要的標準庫,它提供了在基本數(shù)據(jù)類型(如整型、浮點型、布爾型)和字符串之間的轉(zhuǎn)換功能,本文給大家介紹了關(guān)于Go語言字符串轉(zhuǎn)換strconv,需要的朋友可以參考下
    2024-09-09
  • go處理線程之間的交互示例代碼

    go處理線程之間的交互示例代碼

    Go語言以goroutine為核心實現(xiàn)并發(fā)編程,其中線程間交互主要通過Channels、WaitGroup、Mutex和Select實現(xiàn),Channels提供goroutine間的數(shù)據(jù)傳遞,本文給大家介紹go處理線程之間的交互示例代碼,感興趣的朋友一起看看吧
    2024-10-10
  • 詳解Go語言中select語句的常見用法

    詳解Go語言中select語句的常見用法

    這篇文章主要是來和大家介紹一下Go語言中select?語句的常見用法,以及在使用過程中的注意事項,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
    2023-07-07
  • Go語言處理超大字符串型整數(shù)加減經(jīng)典面試詳解

    Go語言處理超大字符串型整數(shù)加減經(jīng)典面試詳解

    這篇文章主要為大家介紹了Go語言處理超大字符串型整數(shù)加減經(jīng)典面試示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-10-10

最新評論

响水县| 绥江县| 搜索| 顺昌县| 塘沽区| 抚远县| 镇巴县| 白山市| 苏尼特左旗| 息烽县| 乡城县| 大悟县| 龙口市| 安远县| 汶川县| 鹤峰县| 明溪县| 鹰潭市| 黑山县| 北宁市| 柯坪县| 高州市| 华容县| 北安市| 错那县| 鄱阳县| 保亭| 马山县| 台州市| 闵行区| 古浪县| 阿巴嘎旗| 原平市| 威远县| 嘉祥县| 芮城县| 久治县| 巴彦县| 永清县| 改则县| 唐海县|