【发布时间】:2020-05-15 10:33:37
【问题描述】:
我正在查看 go.mod 的替换指令:
https://thewebivore.com/using-replace-in-go-mod-to-point-to-your-local-module/
module github.com/pselle/foo
replace github.com/pselle/bar => /Users/pselle/Projects/bar
require (
github.com/pselle/bar v1.0.0
)
在团队中工作时,这非常愚蠢,因为 url 是绝对的,并且会在除您自己的机器之外的任何机器上中断。
有没有办法使用环境变量或相对路径来指定替换指令?像这样的:
replace github.com/pselle/bar => $GOPATH/src/github.com/pselle/bar
或
replace github.com/pselle/bar => ./github.com/pselle/bar
当 PWD 发生变化时使用相对路径非常糟糕,带有 env var 的绝对路径会好得多。
【问题讨论】:
-
你绝对需要提交 go.mod 包括本地路径
replace指令吗?通常这仅用于本地测试并且在提交文件时不包括在内,因此其他开发人员不必担心您如何在您的机器上设置它。 -
嗯,你的意思是我会把 go.mod 放在 gitignore 中?如果它在版本控制中,那么一遍又一遍地在文件中添加/删除同一行并不是很有趣。 NPM 通过使用符号链接可以更好地做到这一点。
-
@Alex 我不认为@Adrian 是在暗示这一点。
go.mod和replace通常用于本地开发/测试(因为真正的 repo 不存在或有新的方法/类型)。是的go.mod应该检查到 git 中。但是,对于本地路径的replace指令可能不应该被排除在 git 之外。
标签: go go-modules