详解golang中 work与 module 的区别与联系

 更新时间:2023年09月27日 08:25:41   作者:demo007x  
Go 模块通常由一个项目或库组成,并包含一组随后一起发布的 Go 包,Go 模块通过允许用户将项目代码放在他们选择的目录中并为每个模块指定依赖项的版本,解决了原始系统的许多问题,本文将给大家介绍一下golang中 work与 module 的区别与联系,需要的朋友可以参考下

一文掌握 golang中 work与 module 的区别与联系

在 1.13 版本中,Go 的作者添加了一种管理 Go 项目所依赖的库的新方法,称为Go 模块go mod。添加 Go 模块是为了满足日益增长的需求,使开发人员更容易维护其依赖项的各种版本,并为开发人员在计算机上组织项目的方式增加更多灵活性。

Go 模块通常由一个项目或库组成,并包含一组随后一起发布的 Go 包。GOPATHGo 模块通过允许用户将项目代码放在他们选择的目录中并为每个模块指定依赖项的版本,解决了原始系统的许多问题。

前面已经详细介绍过了 go module 的使用教程。golang 项目开发如何创建自己的 Module

我们在项目开发的时候不仅仅需要使用别人开发的开源模块,虽然自己公司内部项目的增加回积累

我们在项目开发的时候不仅仅需要使用别人开发的开源模块,虽然自己公司内部项目的增加回积累很多适合自己公司的 golang 的模块来提供给自己公司的其他项目成员使用。比如 sso module 等。

使用 module 开发模块的弊端

我们在使用 golang 的 module 来开发模块的时候需要在项目的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

这样就可以在我们的项目中使用正在开发中的包。

虽然可以满足我们开发包的依赖问题,但是会存在一个严重的问题:一旦我们开发完成将 module 提交到了代码仓库,忘记将 `replace example.com/greetings => ../greetings 其他成员拉去了最新的包,在执行 go mod tidy 就会提示找不到包的问题,因为 replace 指令会得到优先执行,并不会从仓库中拉去对应的模块。

使用 go work 解决 replace 指令的问题

在Go1.18 正式发布后,有了新的模式,那就是 go work 工作区模式(Workspace mode),并不是之前 GOPATH 时代的 Workspace,而是希望在本地开发时支持多 Module

针对 mo module 的问题,Michael Matloob 提出了 Workspace Mode(工作区模式)。相关 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 相关的命令:在命令行执行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 是我们开发的包目录

在 example 目录中执行 go mod 初始化命令:

 ❯ go mod init github.com/example
 go: /Users/oo7/Developer/works/example/go.mod already exists

在 mypkg 目录中执行 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 函数中调用Bar 函数

我们在 example 中新建 main.go 文件,来调用这个 mypkg 包中的 Bar 函数:

main.go

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

执行 go mod tidy

我们在 example 目录与 mypkg 目录中执行 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 目录中执行 go run . 命令,测试我们是否可以正常的函数调用:

 ❯ 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() 函数。

使用 go work 初始化

我们在 work 目录中执行 go work init example mypkg

 ❯ go work init example mypkg
 go: /Users/oo7/Developer/works/go.work already exists

执行完成命令后我们发现在 work 的目录下面生成了 go.work 文件

 go 1.20
 ​
 use (
   ./example
   ./mypkg
 )

example 目录中执行 go run . 命令,测试我们是否可以正常的函数调用:

 ❯ go run .
 this package is mypkg

我们看到代码已经可以正常执行了。不在报错找不到包的问题了。

如果我们执行以下命令:

 ❯ 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 的对比。

当我们开发完成,应该先提交 mypkg 包到 GitHub,然后在 example 下面执行 go get:

go get github.com/mypkg 即可

目前 VSCode 的 go 插件已经支持 workspace,不需要做什么配置就可以使用 go work。同样使用 goland 的同学也是没有任何问题的。

以上就是详解golang中 work与 module 的区别与联系的详细内容,更多关于golang work与module区别的资料请关注脚本之家其它相关文章!

相关文章

  • Linux系统下Go语言开发环境搭建

    Linux系统下Go语言开发环境搭建

    这篇文章主要介绍了Linux系统下Go开发环境搭建,需要的朋友可以参考下
    2022-04-04
  • 详解golang执行Linux shell命令完整场景下的使用方法

    详解golang执行Linux shell命令完整场景下的使用方法

    本文主要介绍了golang执行Linux shell命令完整场景下的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • 从零封装Gin框架配置初始化全局变量

    从零封装Gin框架配置初始化全局变量

    这篇文章主要为大家介绍了从零封装Gin框架配置初始化全局变量,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2024-01-01
  • Go语言基础go fmt命令使用示例详解

    Go语言基础go fmt命令使用示例详解

    这篇文章主要为大家介绍了Go语言基础go fmt命令的使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-11-11
  • Golang协程池的实现与应用

    Golang协程池的实现与应用

    这篇文章主要介绍了Golang协程池的实现与应用,使用协程池的好处是减少在创建和销毁协程上所花的时间以及资源的开销,解决资源不足的问题,需要详细了解可以参考下文
    2023-05-05
  • 详解golang中bufio包的实现原理

    详解golang中bufio包的实现原理

    这篇文章主要介绍了详解golang中bufio包的实现原理,通过分析golang中bufio包的源码,来了解为什么bufio能够提高文件读写的效率和速度
    2018-01-01
  • Go mod包管理工具详解

    Go mod包管理工具详解

    Go mod作为Go语言的官方包管理工具,可以帮助开发者更好地管理包和依赖,提高开发效率和项目可维护性,本文将介绍Go语言的包和依赖管理,以及Go mod的作用和优势,需要的朋友可以参考下
    2023-05-05
  • go内存缓存BigCache封装Entry源码解读

    go内存缓存BigCache封装Entry源码解读

    这篇文章主要为大家介绍了go内存缓存BigCache封装Entry源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • golang实现分页算法实例代码

    golang实现分页算法实例代码

    这篇文章主要给大家介绍了关于golang实现分页算法的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-09-09
  • GO语言不固定参数函数与匿名函数的使用

    GO语言不固定参数函数与匿名函数的使用

    本文主要介绍了GO语言不固定参数函数与匿名函数的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03

最新评论