使用不同的 go.mod 文件构建 Go 项目。

huangapple go评论66阅读模式
英文:

Build a go project with a different go.mod file

问题

我想知道如何使用不同的 go.mod 文件构建一个 Go 项目。假设我想在项目 B 的模块中构建项目 A,而不需要复制文件。这意味着我想要使用项目 B 中的依赖来构建项目 A。

英文:

I would like to know how can I build a go project using a different go.mod file. Suppose I want to build project A inside project B module using project B go.mod file without copying the files around. That means I want to use dependencies in Project B to build Project A.

答案1

得分: 1

手动选项

"模块文件"指的是go.mod和go.sum文件。

  1. 将项目A的模块文件重命名或移动到临时名称/位置。

  2. 将项目B的模块文件复制到项目A中。

  3. 编辑项目A中新复制的go.mod文件,并更改模块名称:

    module github.com/x/b 改为 module github.com/x/a

  4. 在项目A中构建所需的内容。

  5. 删除项目A中的活动模块文件。

  6. 恢复在步骤1中重命名或移动的项目A的正确模块文件。

如果需要经常执行此操作,可以使用shell脚本或批处理文件自动化这些步骤。

使用构建命令

使用go help build命令,我们可以看到构建标志-modfile的用法:

 -modfile file
                在模块感知模式下,读取(可能写入)替代的go.mod文件,而不是模块根目录中的文件。仍然必须存在名为"go.mod"的文件以确定模块根目录,但不会访问它。当指定了-modfile时,还会使用替代的go.sum文件:其路径是通过修剪.mod扩展名并添加.sum来获得的。

使用这个标志,我们可以直接使用替代的模块文件集来构建项目A中的内容。

首先,标志描述表明它可能会写入go.mod文件,因此最好还是创建项目B的模块文件的副本来执行此操作。

其次,如果项目A和项目B在其模块文件中声明了不同的模块名称,并且项目A中的包导入了项目A中的其他包,那么使用项目B的模块文件将会有问题。模块名称决定了模块中包的导入路径,因此更改它可能会破坏导入。

因此,最佳做法仍然是:

  1. 复制项目B的模块文件。
  2. 在副本中更改模块名称。

然后,您可以像这样使用构建命令来构建项目A:

go build -modfile path/to/projectb/go.mod

英文:

Manual option

"Module files" refers to both go.mod and go.sum

  1. Rename or move project A's module files to some temporary names / location
  2. Copy project B's module files into project A
  3. Edit the newly copied go.mod file in project A, and change the module name:

module github.com/x/b changes to module github.com/x/a

  1. Build whatever you need to build in project A
  2. Delete the active module files in project A
  3. Restore the proper module files for project A that you renamed or moved in step 1

These steps could be automated with a shell script or batch file if you need to do it often.

With build command

Using the go help build command, we can see the build flag -modfile

 -modfile file
                in module aware mode, read (and possibly write) an alternate go.mod
                file instead of the one in the module root directory. A file named
                "go.mod" must still be present in order to determine the module root
                directory, but it is not accessed. When -modfile is specified, an
                alternate go.sum file is also used: its path is derived from the
                -modfile flag by trimming the ".mod" extension and appending ".sum".

Using this, we can directly use an alternative set of module files to build things in project A.

First, the flag description indicates that it may write to the go.mod file, so it's probably still a good idea to create a copy of project B's module files to do this.

Second, using project B's module file is going to be a problem if: 1. project A and project B have a different module name declared in their module file, and 2. packages in project A import other packages in project A. The module name determines what the import path of packages in the module will be, so changing it could break imports.

So the best practice should still be to:

  1. Make a copy of project B's module files
  2. Change the module name in the copy

Then you can run the build command like this to build in project A:

go build -modfile path/to/projectb/go.mod

答案2

得分: -1

首先,在某个地方创建一个名为b的文件夹。然后在b文件夹内创建一个名为a的文件夹。然后创建b/b.go文件:

package b

const Something = 1

接下来创建b/a/a.go文件:

package a
import "b"

func something() int {
   return b.Something
}

然后返回到b文件夹,并执行go mod init b命令。完成。

英文:

First, make a folder b somewhere. Then make a folder a inside b. Then make
b/b.go:

package b

const Something = 1

Then make b/a/a.go:

package a
import "b"

func something() int {
   return b.Something
}

Then go back to b folder, and do go mod init b. Done.

huangapple
  • 本文由 发表于 2021年6月13日 08:41:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/67953978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定