詳解golang中?work與?module?的區(qū)別與聯(lián)系
一文掌握 golang中 work與 module 的區(qū)別與聯(lián)系
在 1.13 版本中,Go 的作者添加了一種管理 Go 項(xiàng)目所依賴的庫(kù)的新方法,稱為Go 模塊go mod
。添加 Go 模塊是為了滿足日益增長(zhǎng)的需求,使開(kāi)發(fā)人員更容易維護(hù)其依賴項(xiàng)的各種版本,并為開(kāi)發(fā)人員在計(jì)算機(jī)上組織項(xiàng)目的方式增加更多靈活性。
Go 模塊通常由一個(gè)項(xiàng)目或庫(kù)組成,并包含一組隨后一起發(fā)布的 Go 包。GOPATHGo 模塊通過(guò)允許用戶將項(xiàng)目代碼放在他們選擇的目錄中并為每個(gè)模塊指定依賴項(xiàng)的版本,解決了原始系統(tǒng)的許多問(wèn)題。
前面已經(jīng)詳細(xì)介紹過(guò)了 go module 的使用教程。golang 項(xiàng)目開(kāi)發(fā)如何創(chuàng)建自己的 Module
我們?cè)陧?xiàng)目開(kāi)發(fā)的時(shí)候不僅僅需要使用別人開(kāi)發(fā)的開(kāi)源模塊,雖然自己公司內(nèi)部項(xiàng)目的增加回積累
我們?cè)陧?xiàng)目開(kāi)發(fā)的時(shí)候不僅僅需要使用別人開(kāi)發(fā)的開(kāi)源模塊,雖然自己公司內(nèi)部項(xiàng)目的增加回積累很多適合自己公司的 golang 的模塊來(lái)提供給自己公司的其他項(xiàng)目成員使用。比如 sso module 等。
使用 module 開(kāi)發(fā)模塊的弊端
我們?cè)谑褂?golang 的 module 來(lái)開(kāi)發(fā)模塊的時(shí)候需要在項(xiàng)目的go.mod
文件中引入對(duì)應(yīng)的項(xiàng)目,但是golang 默認(rèn)會(huì)去相對(duì)應(yīng)的地址去拉去對(duì)應(yīng)的包,但是這個(gè)時(shí)候我們的module 并沒(méi)有提交到自己的倉(cāng)庫(kù)中。那么這個(gè)時(shí)候golang 就會(huì)報(bào)錯(cuò),找不到對(duì)應(yīng)的 package。
那么這個(gè)時(shí)候應(yīng)該怎么做呢?一般的做法就是在 go.mod
文件中添加一條指令 replace
?module example.com/hello ?? ?go 1.20 ?? ?replace example.com/greetings => ../greetings ?? ?require example.com/greetings v0.0.0-00010101000000-000000000000
這樣就可以在我們的項(xiàng)目中使用正在開(kāi)發(fā)中的包。
雖然可以滿足我們開(kāi)發(fā)包的依賴問(wèn)題,但是會(huì)存在一個(gè)嚴(yán)重的問(wèn)題:一旦我們開(kāi)發(fā)完成將 module 提交到了代碼倉(cāng)庫(kù),忘記將 `replace example.com/greetings => ../greetings
其他成員拉去了最新的包,在執(zhí)行 go mod tidy
就會(huì)提示找不到包的問(wèn)題,因?yàn)?replace
指令會(huì)得到優(yōu)先執(zhí)行,并不會(huì)從倉(cāng)庫(kù)中拉去對(duì)應(yīng)的模塊。
使用 go work 解決 replace 指令的問(wèn)題
在Go1.18 正式發(fā)布后,有了新的模式,那就是 go work
工作區(qū)模式(Workspace mode),并不是之前 GOPATH
時(shí)代的 Workspace
,而是希望在本地開(kāi)發(fā)時(shí)支持多 Module
。
針對(duì) mo module 的問(wèn)題,Michael Matloob 提出了 Workspace Mode(工作區(qū)模式)。相關(guān) issue 討論:cmd/go: add a workspace mode , Proposal,感興趣的可以去參閱。
所以要想使用 go work,那基本的要求就是你的 golang version 必須是 golang 1.18
以上的版本
?? go version ?go version go1.20.7 darwin/amd64
我們先看看 go wokr 相關(guān)的命令:在命令行執(zhí)行go help work
?? go help work ?Work provides access to operations on workspaces. ?? ?Note that support for workspaces is built into many other commands, not ?just 'go work'. ?? ?See 'go help modules' for information about Go's module system of which ?workspaces are a part. ?? ?See https://go.dev/ref/mod#workspaces for an in-depth reference on ?workspaces. ?? ?See https://go.dev/doc/tutorial/workspaces for an introductory ?tutorial on workspaces. ?? ?A workspace is specified by a go.work file that specifies a set of ?module directories with the "use" directive. These modules are used as ?root modules by the go command for builds and related operations. A ?workspace that does not specify modules to be used cannot be used to do ?builds from local modules. ?? ?go.work files are line-oriented. Each line holds a single directive, ?made up of a keyword followed by arguments. For example: ?? ? go 1.18 ?? ? use ../foo/bar ? use ./baz ?? ? replace example.com/foo v1.2.3 => example.com/bar v1.4.5 ?? ?The leading keyword can be factored out of adjacent lines to create a block, ?like in Go imports. ?? ? use ( ? ../foo/bar ? ./baz ? ) ?? ?The use directive specifies a module to be included in the workspace's ?set of main modules. The argument to the use directive is the directory ?containing the module's go.mod file. ?? ?The go directive specifies the version of Go the file was written at. It ?is possible there may be future changes in the semantics of workspaces ?that could be controlled by this version, but for now the version ?specified has no effect. ?? ?The replace directive has the same syntax as the replace directive in a ?go.mod file and takes precedence over replaces in go.mod files. It is ?primarily intended to override conflicting replaces in different workspace ?modules. ?? ?To determine whether the go command is operating in workspace mode, use ?the "go env GOWORK" command. This will specify the workspace file being ?used. ?? ?Usage: ?? ? go work <command> [arguments] ?? ?The commands are: ?? ? edit ? ? ? edit go.work from tools or scripts ? init ? ? ? initialize workspace file ? sync ? ? ? sync workspace build list to modules ? use ? ? ? ? add modules to workspace file ?? ?Use "go help work <command>" for more information about a command.
當(dāng)前的目錄結(jié)構(gòu)是:
?? tree ?├── example ?└── mypkg
初始化 go mod
example 是我們的項(xiàng)目目錄, mypkg 是我們開(kāi)發(fā)的包目錄
在 example 目錄中執(zhí)行 go mod
初始化命令:
?? go mod init github.com/example ?go: /Users/oo7/Developer/works/example/go.mod already exists
在 mypkg 目錄中執(zhí)行 go mod
初始化命令:
?? go mod init github.com/mypkg ?go: /Users/oo7/Developer/works/mypkg/go.mod already exists
編寫(xiě) Bar()
在 mypkg
目錄中新建文件 demo.go
添加內(nèi)容:
?package mypkg ?? ?func Bar() { ? println("this package is mypkg") ?}
Main 函數(shù)中調(diào)用Bar 函數(shù)
我們?cè)?example 中新建 main.go 文件,來(lái)調(diào)用這個(gè) mypkg 包中的 Bar 函數(shù):
main.go
?package main ?? ?import ( ? "github.com/mypkg" ?) ?? ?func main() { ? mypkg.Bar() ?}
執(zhí)行 go mod tidy
我們?cè)?example 目錄與 mypkg 目錄中執(zhí)行 go mod tidy
命令, 確保 go.mod 與模塊中的源代碼匹配。
?? cd example/ ?? go mod tidy ?go: finding module for package github.com/mypkg ?github.com/example imports ? ? ? ? ?github.com/mypkg: cannot find module providing package github.com/mypkg: invalid github.com import path "github.com/mypkg" ?? cd ../mypkg/ ?? go mod tidy ?~/Developer/works/mypkg ? ? ?
測(cè)試代碼
我們?cè)?example 目錄中執(zhí)行 go run .
命令,測(cè)試我們是否可以正常的函數(shù)調(diào)用:
?? cd example/ ?? go run . ?main.go:4:2: no required module provides package github.com/mypkg; to add it: ? ? ? ? ?go get github.com/mypkg
從相應(yīng)結(jié)果看 go 并沒(méi)有找到 mypkg 包,以及 mypkg.Bar()
函數(shù)。
使用 go work 初始化
我們?cè)?work 目錄中執(zhí)行 go work init example mypkg
?? go work init example mypkg ?go: /Users/oo7/Developer/works/go.work already exists
執(zhí)行完成命令后我們發(fā)現(xiàn)在 work 的目錄下面生成了 go.work
文件
?go 1.20 ?? ?use ( ? ./example ? ./mypkg ?)
example 目錄中執(zhí)行 go run .
命令,測(cè)試我們是否可以正常的函數(shù)調(diào)用:
?? go run . ?this package is mypkg
我們看到代碼已經(jīng)可以正常執(zhí)行了。不在報(bào)錯(cuò)找不到包的問(wèn)題了。
如果我們執(zhí)行以下命令:
?? GOWORK=off go run main.go ?main.go:4:2: no required module provides package github.com/mypkg; to add it: ? ? ? ? go get github.com/mypkg
將 GOWORK
設(shè)置關(guān)閉,同樣是報(bào)錯(cuò)。
總結(jié)
以上我們已經(jīng)體會(huì)到了 go work 的作用,以及與 replace 的對(duì)比。
當(dāng)我們開(kāi)發(fā)完成,應(yīng)該先提交 mypkg 包到 GitHub,然后在 example 下面執(zhí)行 go get:
go get github.com/mypkg
即可
目前 VSCode 的 go 插件已經(jīng)支持 workspace,不需要做什么配置就可以使用 go work。同樣使用 goland 的同學(xué)也是沒(méi)有任何問(wèn)題的。
以上就是詳解golang中 work與 module 的區(qū)別與聯(lián)系的詳細(xì)內(nèi)容,更多關(guān)于golang work與module區(qū)別的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Goland使用Go Modules創(chuàng)建/管理項(xiàng)目的操作
- Linux中Go環(huán)境配置和GoModule常用操作
- Go?modules?replace解決Go依賴引用問(wèn)題
- go modules中replace使用方法
- GoLang中Module的基本使用方法
- go module構(gòu)建項(xiàng)目的實(shí)現(xiàn)
- 重學(xué)Go語(yǔ)言之如何使用Modules
- 淺析Go項(xiàng)目中的依賴包管理與Go?Module常規(guī)操作
- Go Module常用命令及如何使用Go Module
- Go Module依賴管理的實(shí)現(xiàn)
相關(guān)文章
Golang項(xiàng)目搭配nginx部署反向代理負(fù)載均衡講解
這篇文章主要為大家介紹了Golang項(xiàng)目搭配nginx部署正反向代理負(fù)載均衡講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04go程序測(cè)試CPU占用率統(tǒng)計(jì)ps?vs?top兩種不同方式對(duì)比
這篇文章主要為大家介紹了go程序測(cè)試CPU占用率統(tǒng)計(jì)ps?vs?top兩種不同方式對(duì)比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Golang 實(shí)現(xiàn) RTP音視頻傳輸示例詳解
這篇文章主要為大家介紹了Golang實(shí)現(xiàn)RTP音視頻傳輸?shù)氖纠斀?,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07golang使用map支持高并發(fā)的方法(1000萬(wàn)次操作14ms)
這篇文章主要介紹了golang使用map支持高并發(fā)的方法(1000萬(wàn)次操作14ms),本文給大家詳細(xì)講解,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11