Can I specify a replace directive elsewhere other than go.mod for sharing with other developers?

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

Can I specify a replace directive elsewhere other than go.mod for sharing with other developers?

问题

go.mod文件中的replace指令是一种本地配置选项,不同的开发者可能将本地模块源代码放在不同的位置。

将这个选项包含在一个必须提交到仓库的文件中(无论是私有还是公共仓库),感觉上就不太对。

有没有办法在go.mod之外的其他地方指定这个选项?

示例:
https://github.com/Drean64/c64/blob/master/src/go.mod#L5

module github.com/Drean64/c64
go 1.18
replace github.com/Drean64/cpu6502 => ../../cpu6502/src
英文:

go.mod's replace directive is a local configuration option, different developers could have the local module source in different locations.

It just feels wrong including this option in a file that has to be committed to a repo from which others can use the module (be it private or public).

Is there a way to specify this somewhere else than in go.mod?

Example:
https://github.com/Drean64/c64/blob/master/src/go.mod#L5

module github.com/Drean64/c64
go 1.18
replace github.com/Drean64/cpu6502 => ../../cpu6502/src

答案1

得分: 2

当你想使用本地模块时,可以使用替换指令作为临时解决方案,但我更喜欢使用构建标志。下面的-modfile是一个好的选择,你可以在构建或运行程序时使用它。

例如:go run -modfile=local.mod main.go

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

我只在需要临时解决方案时使用替换指令。

英文:

replace directive temporary solution when you want to use local modules but I prefer to use build flags, below -modfile is good and you can use it while building or running the program.

example : go run -modfile=local.mod main.go

-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".

I do use replace directive only when needs a temporary solution.

答案2

得分: 1

不同的开发者可能将本地模块源代码放在不同的位置。

通过使用相对路径,您可以引用主存储库项目的子模块文件夹,这意味着所有开发者都可以从相同的本地replace指令中受益。

是否有一种方法可以在go.mod之外的其他地方指定这个呢?

似乎没有,replace指令go.mod相关联,并且:

replace指令仅适用于主模块的go.mod文件,在其他模块中将被忽略。

英文:

> different developers could have the local module source in different locations.

By using a relative path, you could reference a folder which is a submodule of your main repository project, which means all developer would benefit from the same local replace directive.

> Is there a way to specify this somewhere else than in go.mod?

It does not seem to be, the replace directive is linked to go.mod, and:

> replace directives only apply in the main module’s go.mod file and are ignored in other modules.

huangapple
  • 本文由 发表于 2022年5月21日 14:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/72327278.html
匿名

发表评论

匿名网友

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

确定