英文:
How to ignore replace directive in go.mod
问题
我在本地开发时使用了"replace"语句。所以我的go.mod文件看起来像这样:
require (
gorm.io/gorm v1.21.11
github.com/mypackages/session v1.1.0
)
replace (
github.com/mypackages/session => ./../session
)
但是当我提交代码并将其部署到生产环境时,我不需要"replace"语句,所以我需要在每次"git commit"时注释掉这行"replace"代码,然后再取消注释。有没有办法在生产环境中忽略"replace"语句?
英文:
I'm using "replace" statement while do my local development. So my go.mod looks like this:
require (
gorm.io/gorm v1.21.11
github.com/mypackages/session v1.1.0
)
replace (
github.com/mypackages/session => ./../session
)
But I have no need in "replace" when I git commit
my changes and deploy code to production, so I need to comment this line of replace code on each git commit
and uncomment it then. Is there a way to ignore the "replace" statement on a production environment?
答案1
得分: 5
replace
在环境中是不能被忽略的,因为它在依赖解析阶段使用,在构建之前,而构建又远在它在生产环境中被执行之前。但是回答根本问题,不,你不能“忽略”这个指令。如果它存在,就是存在的。
英文:
replace
can't be ignored in an environment, because it's used at dependency resolution time, which comes before build, which is long before it ever gets executed in production. But to answer the root question, no, you can't "ignore" the directive. If it's there, it's there.
答案2
得分: 5
虽然 @Adrian 是正确的,Go 中没有办法实现这个功能,但我认为这个问题与 Go 本身关系不大,更多是关于 Git。你可以使用内容过滤器来忽略文件中的特定部分。请参考这个 Stack Overflow 回答获取更多信息。
英文:
While @Adrian is correct in that there is no way to accomplish this in Go, I think this question is less about Go and more about Git. You can ignore a specific part of your file using content filters. See this SO answer for more information.
答案3
得分: 1
你可以在本地拥有一个模块文件的本地版本(例如go.local.mod),然后告诉go命令使用它:
go build -modfile=go.local.mod main.go
英文:
Have a local version of the mod file (e.g. go.local.mod) and then you can tell the go command to use it:
go build -modfile=go.local.mod main.go
答案4
得分: 0
你可以编写一个 pre commit hook,用于检测你的 go.mod 文件是否包含 replace。
Pre commit hook 代码 -> https://gist.github.com/MohitVachhani/e331321bb6f8c3e5e26b0cb4ab792c55
请查看并告诉我是否有任何改进!
英文:
You can code a pre commit hook, which will detect whether your go.mod file contains replace or not.
Pre commit hook code -> https://gist.github.com/MohitVachhani/e331321bb6f8c3e5e26b0cb4ab792c55
Please go through this and let me know if any improvements you have!
答案5
得分: 0
配方:
将以下内容添加到项目的.git/config
文件中:
[filter "goreplace"]
smudge = sed 's~//replace~replace~'
clean = sed 's~replace~//replace~'
或者添加到全局配置中:
git config --global filter.goreplace.smudge "sed 's~//replace~replace~'"
git config --global filter.goreplace.clean "sed 's~replace~//replace~'"
然后将以下内容添加到.gitattributes
文件中:
*.mod filter=goreplace
请注意,这只对您有效,而不适用于所有贡献者。它只是将“replace”语句注释掉,您也可以将其删除,但这需要更多的sed魔法 ;)。
英文:
The recipe:
Add this to .git/config
of your project
[filter "goreplace"]
smudge = sed 's~//replace~replace~'
clean = sed 's~replace~//replace~'
alternative add global:
git config --global filter.goreplace.smudge "sed 's~//replace~replace~'"
git config --global filter.goreplace.clean "sed 's~replace~//replace~'"
and then add (to) the .gitattributes
*.mod filter=goreplace
Notice it just works for you not for all contributors. Also it only comment out the 'replace' statement you could also remove it but this would require more sed magic ;).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论