Can I specify a replace directive elsewhere other than go.mod for sharing with other developers?

Issue

go.mod‘s replace directive is a local configuration option, different developers could have the local module source in different locations.

It just feels wrong including this option in a file that has to be committed to a repo from which others can use the module (be it private or public).

Is there a way to specify this somewhere else than in go.mod?

Example:
https://github.com/Drean64/c64/blob/master/src/go.mod#L5

module github.com/Drean64/c64
go 1.18
replace github.com/Drean64/cpu6502 => ../../cpu6502/src

Solution

replace directive temporary solution when you want to use local modules but I prefer to use build flags, below -modfile is good and you can use it while building or running the program.

example : go run -modfile=local.mod main.go

-modfile file
        in module aware mode, read (and possibly write) an alternate go.mod
        file instead of the one in the module root directory. A file named
        "go.mod" must still be present in order to determine the module root
        directory, but it is not accessed. When -modfile is specified, an
        alternate go.sum file is also used: its path is derived from the
        -modfile flag by trimming the ".mod" extension and appending ".sum".

I do use replace directive only when needs a temporary solution.

Answered By – Mandeep Gill

Answer Checked By – Jay B. (GoLangFix Admin)

Leave a Reply

Your email address will not be published.