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

Time:05-21

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

CodePudding user response:

different developers could have the local module source in different locations.

By using a relative path, you could reference a folder which is a submodule of your main repository project, which means all developer would benefit from the same local replace directive.

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

It does not seem to be, the replace directive is linked to go.mod, and:

replace directives only apply in the main module’s go.mod file and are ignored in other modules.

CodePudding user response:

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.

  • Related