简体   繁体   中英

How to configure go mod to have a require version based on environment variable?

I have two internal projects.

Project1 - go.mod file

go github.com/company/project1
 
go 1.16

require github.com/company/project2 v1.1.0 // indirect

Project2 - go.mod file

go github.com/company/project2
 
go 1.16

Project1 is dependent on Project2 based on the above go.mod file.

When Project2 moves across multiple environments it needs to be dependent upon different version of Project1.
For instance, based on environment variable:

  • If ENVIRONMENT = LOAD

    Project1 depends on 1.1.0-<ENVIRONMENT> version of Project2
  • If ENVIRONMENT = TEST

    Project1 depends on 1.1.0-<ENVIRONMENT>.<DATE> version of Project2

Can this be done dynamically without modifying the go.mod file everytime?

Can this be done dynamically without modifying the go.mod file everytime?

Not that I know of: your deployment process should include a go mod replace (as seen here ) in order to update the project2 dependency, depending on the current deployment environment.

If that deployment involves a git clone, then you might consider content filter driver ( illustrated here ) to do that change automatically on checkout.

But if this is done through Go, then a go mod replace is better.


Note: issue 27824 ( go.mod : using environment variable for replacement) was closed in 2019, because of implementation complexity.
Issue 33586 ( go.mod : support ~ in replace statements) is still opened.

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