Learn to Use replace in go.mod

Published: 02 October 2021
on channel: rwxrob
2,174
38

The replace keyword in go.mod allows you to work on several packages, with different repos, at the same time, or, to test changes locally before pushing them.

Unfortunately, it is rather easy to leave the replace in your go.mod file and forget and commit it. Until rather recently, that meant other projects depending on your module would fail to compile trying to locate your local path on their build machines. These days go won't even compile or install the module if there is a replace in the go.mod file.

Even so, committing to a production repo with replace could be embarrassing so might want to add the following .git/hooks/pre-commit hook (don't forget to chmod +x) to prevent any commit of go.mod that has the word replace in it, which you could easily modify to check that it isn't commented out (// replace should be allowed). This script checks for jq and if not found uses grep instead:

[code snipped]

Tags:

#golang #tips #replace

Thanks sponsors, subs, and followers:

GitHub: https://github.com/sponsors/rwxrob
Twitch:   / rwxrob  
Twitter:   / rwxrob  
Discord:   / discord  

See http://rwxrob.tv/schedule for scheduled streams.

[ZETID:20211002150304]
https://github.com/rwxrob/zet/tree/ma...


Watch video Learn to Use replace in go.mod online without registration, duration hours minute second in high quality. This video was added by user rwxrob 02 October 2021, don't forget to share it with your friends and acquaintances, it has been viewed on our site 2,174 once and liked it 38 people.