简体   繁体   中英

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:

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?

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.

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.

Have a local version of the mod file (eg go.local.mod) and then you can tell the go command to use it:

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.

Pre commit hook code -> https://gist.github.com/MohitVachhani/e331321bb6f8c3e5e26b0cb4ab792c55

Please go through this and let me know if any improvements you have!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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