2

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?

Flimzy
  • 68,325
  • 15
  • 126
  • 165
sn0rk
  • 61
  • 4
  • For what it's worth, https://golang.org/issue/45713 proposes an alternative to `replace` for local development, in a form that would be easier to `.gitignore` (and/or easier to override for production use by passing `-workfile=off`). – bcmills Aug 02 '21 at 14:34
  • 1
    if the replace is there only because you need it in your local development, [you could use an alternate go.mod file instead](https://stackoverflow.com/questions/68764637/how-to-use-an-alternate-go-mod-file-for-local-development) – blackgreen Jun 01 '22 at 21:23

3 Answers3

5

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.

Adrian
  • 38,231
  • 5
  • 90
  • 87
  • Thanks for your answer. Are you ever faced with that problem? Maybe you know a workaround solution? – sn0rk Jul 25 '21 at 12:34
4

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.

Clark McCauley
  • 1,093
  • 3
  • 15
0

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
  • 1
    When linking to your own site or content (or content that you are affiliated with), you [must disclose your affiliation _in the answer_](/help/promotion) in order for it not to be considered spam. Having the same text in your username as the URL or mentioning it in your profile is not considered sufficient disclosure under Stack Exchange policy. – cigien Jan 22 '22 at 14:32