亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解golang中?work與?module?的區(qū)別與聯(lián)系

 更新時間:2023年09月27日 08:25:41   作者:demo007x  
Go?模塊通常由一個項目或庫組成,并包含一組隨后一起發(fā)布的?Go?包,Go?模塊通過允許用戶將項目代碼放在他們選擇的目錄中并為每個模塊指定依賴項的版本,解決了原始系統(tǒng)的許多問題,本文將給大家介紹一下golang中?work與?module?的區(qū)別與聯(lián)系,需要的朋友可以參考下

一文掌握 golang中 work與 module 的區(qū)別與聯(lián)系

在 1.13 版本中,Go 的作者添加了一種管理 Go 項目所依賴的庫的新方法,稱為Go 模塊go mod。添加 Go 模塊是為了滿足日益增長的需求,使開發(fā)人員更容易維護其依賴項的各種版本,并為開發(fā)人員在計算機上組織項目的方式增加更多靈活性。

Go 模塊通常由一個項目或庫組成,并包含一組隨后一起發(fā)布的 Go 包。GOPATHGo 模塊通過允許用戶將項目代碼放在他們選擇的目錄中并為每個模塊指定依賴項的版本,解決了原始系統(tǒng)的許多問題。

前面已經詳細介紹過了 go module 的使用教程。golang 項目開發(fā)如何創(chuàng)建自己的 Module

我們在項目開發(fā)的時候不僅僅需要使用別人開發(fā)的開源模塊,雖然自己公司內部項目的增加回積累

我們在項目開發(fā)的時候不僅僅需要使用別人開發(fā)的開源模塊,雖然自己公司內部項目的增加回積累很多適合自己公司的 golang 的模塊來提供給自己公司的其他項目成員使用。比如 sso module 等。

使用 module 開發(fā)模塊的弊端

我們在使用 golang 的 module 來開發(fā)模塊的時候需要在項目的go.mod文件中引入對應的項目,但是golang 默認會去相對應的地址去拉去對應的包,但是這個時候我們的module 并沒有提交到自己的倉庫中。那么這個時候golang 就會報錯,找不到對應的 package。

那么這個時候應該怎么做呢?一般的做法就是在 go.mod 文件中添加一條指令 replace

?module example.com/hello
??
?go 1.20
??
?replace example.com/greetings => ../greetings
??
?require example.com/greetings v0.0.0-00010101000000-000000000000

這樣就可以在我們的項目中使用正在開發(fā)中的包。

雖然可以滿足我們開發(fā)包的依賴問題,但是會存在一個嚴重的問題:一旦我們開發(fā)完成將 module 提交到了代碼倉庫,忘記將 `replace example.com/greetings => ../greetings 其他成員拉去了最新的包,在執(zhí)行 go mod tidy 就會提示找不到包的問題,因為 replace 指令會得到優(yōu)先執(zhí)行,并不會從倉庫中拉去對應的模塊。

使用 go work 解決 replace 指令的問題

在Go1.18 正式發(fā)布后,有了新的模式,那就是 go work 工作區(qū)模式(Workspace mode),并不是之前 GOPATH 時代的 Workspace,而是希望在本地開發(fā)時支持多 Module。

針對 mo module 的問題,Michael Matloob 提出了 Workspace Mode(工作區(qū)模式)。相關 issue 討論:cmd/go: add a workspace modeProposal,感興趣的可以去參閱。

所以要想使用 go work,那基本的要求就是你的 golang version 必須是 golang 1.18 以上的版本

?? go version
?go version go1.20.7 darwin/amd64

我們先看看 go wokr 相關的命令:在命令行執(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.

當前的目錄結構是:

?? tree
?├── example
?└── mypkg

初始化 go mod

example 是我們的項目目錄, mypkg 是我們開發(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

編寫 Bar()

在 mypkg 目錄中新建文件 demo.go 添加內容:

?package mypkg
??
?func Bar() {
?  println("this package is mypkg")
?}

Main 函數(shù)中調用Bar 函數(shù)

我們在 example 中新建 main.go 文件,來調用這個 mypkg 包中的 Bar 函數(shù):

main.go

?package main
??
?import (
?  "github.com/mypkg"
?)
??
?func main() {
?  mypkg.Bar()
?}

執(zhí)行 go mod tidy

我們在 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 ? ? ?

測試代碼

我們在 example 目錄中執(zhí)行 go run . 命令,測試我們是否可以正常的函數(shù)調用:

?? cd example/
?? go run .
?main.go:4:2: no required module provides package github.com/mypkg; to add it:
? ? ? ? ?go get github.com/mypkg

從相應結果看 go 并沒有找到 mypkg 包,以及 mypkg.Bar() 函數(shù)。

使用 go work 初始化

我們在 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 . 命令,測試我們是否可以正常的函數(shù)調用:

?? go run .
?this package is mypkg

我們看到代碼已經可以正常執(zhí)行了。不在報錯找不到包的問題了。

如果我們執(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 設置關閉,同樣是報錯。

總結

以上我們已經體會到了 go work 的作用,以及與 replace 的對比。

當我們開發(fā)完成,應該先提交 mypkg 包到 GitHub,然后在 example 下面執(zhí)行 go get:

go get github.com/mypkg 即可

目前 VSCode 的 go 插件已經支持 workspace,不需要做什么配置就可以使用 go work。同樣使用 goland 的同學也是沒有任何問題的。

以上就是詳解golang中 work與 module 的區(qū)別與聯(lián)系的詳細內容,更多關于golang work與module區(qū)別的資料請關注腳本之家其它相關文章!

相關文章

  • Golang slice切片操作之切片的追加、刪除、插入等

    Golang slice切片操作之切片的追加、刪除、插入等

    這篇文章主要介紹了Golang slice切片操作之切片的追加、刪除、插入等,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • golang中日期操作之日期格式化及日期轉換

    golang中日期操作之日期格式化及日期轉換

    在編程中,程序員會經常使用到日期相關操作,下面這篇文章主要給大家介紹了關于golang中日期操作之日期格式化及日期轉換的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • Golang項目搭配nginx部署反向代理負載均衡講解

    Golang項目搭配nginx部署反向代理負載均衡講解

    這篇文章主要為大家介紹了Golang項目搭配nginx部署正反向代理負載均衡講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪
    2022-04-04
  • go程序測試CPU占用率統(tǒng)計ps?vs?top兩種不同方式對比

    go程序測試CPU占用率統(tǒng)計ps?vs?top兩種不同方式對比

    這篇文章主要為大家介紹了go程序測試CPU占用率統(tǒng)計ps?vs?top兩種不同方式對比,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • 深入理解Go中defer的機制

    深入理解Go中defer的機制

    本文主要介紹了Go中defer的機制,包括執(zhí)行順序、參數(shù)預計算、閉包和與返回值的交互,具有一定的參考價值,感興趣的可以了解一下
    2025-02-02
  • Golang 實現(xiàn) RTP音視頻傳輸示例詳解

    Golang 實現(xiàn) RTP音視頻傳輸示例詳解

    這篇文章主要為大家介紹了Golang實現(xiàn)RTP音視頻傳輸?shù)氖纠斀?,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • Go語言中你不知道的Interface詳解

    Go語言中你不知道的Interface詳解

    對于go語言來說,設計最精妙的應該是interface了,直白點說interface是一組method的組合。下面這篇文章主要給大家介紹了關于Go語言中你不知道的Interface的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2018-02-02
  • 修改并編譯golang源碼的操作步驟

    修改并編譯golang源碼的操作步驟

    這篇文章主要介紹了修改并編譯golang源碼的操作步驟,本文給大家介紹的非常詳細,需要的朋友可以參考下
    2021-07-07
  • golang使用map支持高并發(fā)的方法(1000萬次操作14ms)

    golang使用map支持高并發(fā)的方法(1000萬次操作14ms)

    這篇文章主要介紹了golang使用map支持高并發(fā)的方法(1000萬次操作14ms),本文給大家詳細講解,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • 詳解Go 語言中的比較操作符

    詳解Go 語言中的比較操作符

    這篇文章專注于 6 個操作符,==,!=,<,<=,> 和 >=。我們將深入探討它們的語法和用法的細微差別,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-08-08

最新評論