简体   繁体   English

如何忽略 go.mod 中的替换指令

[英]How to ignore replace directive in go.mod

I'm using "replace" statement while do my local development.我在进行本地开发时使用“替换”语句。 So my go.mod looks like this:所以我的 go.mod 看起来像这样:

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.但是当我git commit我的更改并将代码部署到生产环境时,我不需要“替换”,所以我需要在每个git commit上注释这行替换代码,然后取消注释。 Is there a way to ignore the "replace" statement on a production environment?有没有办法忽略生产环境中的“替换”语句?

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. replace环境中不能被忽略,因为它在依赖解析时间使用,在构建之前,这是在生产中执行之前很久。 But to answer the root question, no, you can't "ignore" the directive.但是要回答根本问题,不,您不能“忽略”该指令。 If it's there, it's there.如果它在那里,它就在那里。

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.虽然@Adrian 是正确的,因为在 Go 中无法实现这一点,但我认为这个问题与 Go 相关,而与 Git 相关。 You can ignore a specific part of your file using content filters.您可以使用内容过滤器忽略文件的特定部分。 See this SO answer for more information.有关更多信息,请参阅此 SO 答案

Have a local version of the mod file (eg go.local.mod) and then you can tell the go command to use it:拥有一个本地版本的 mod 文件(例如 go.local.mod),然后你可以告诉 go 命令使用它:

go build -modfile=go.local.mod main.go

Can also set the env variable so you don't have to type it out each time. 还可以设置环境变量,这样您就不必每次都输入它。

You can code a pre commit hook, which will detect whether your go.mod file contains replace or not.您可以编写一个预提交挂钩,它将检测您的 go.mod 文件是否包含替换。

Pre commit hook code -> https://gist.github.com/MohitVachhani/e331321bb6f8c3e5e26b0cb4ab792c55预提交挂钩代码 -> https://gist.github.com/MohitVachhani/e331321bb6f8c3e5e26b0cb4ab792c55

Please go through this and let me know if any improvements you have!请通过此拨打 go,如果您有任何改进,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM