Placeholders in import statement in go

  • Feb 28, 2022 at 09:28
  • 49 times
Based on the data dump provided by the Stack Exchange Network. Link to the original question.


-1
0

I am using the go language and I want to understand the alternative to one scenario that I am facing.

we have import statements in the files as

import "github.com/Dir1/Dir2/v101/ServiceName"

I have a dependency on SDK which follows the directory structure like this. It has version_no directory.

Problem - Every-time the SDK version is updated we have to replace the import statement with appropriate version.

Currently in project it is achieved using the sed command which is very heavy operation as we have thousands of files. Ex Changing "github.com/Dir1/Dir2/v101/ServiceName" ==> "github.com/Dir1/Dir2/v102/ServiceName"

SDK team will not provide any support so we have to find the good way to resolve this.

I need your suggestion about how this can be achieved.

1 Answers


0

Add this in the go.mod file:

replace github.com/Dir1/Dir2/v101/ServiceName => github.com/Dir1/Dir2/v102/ServiceName

Now, you can keep using github.com/Dir1/Dir2/v101/ServiceName everywhere, and update this replace directive every time the version needs to be updated. For example, if the next version is v103, update this:

replace github.com/Dir1/Dir2/v101/ServiceName => github.com/Dir1/Dir2/v103/ServiceName
Copied ✓