gogofmt

Fixing go imports to relative instead of absolute path


Is there a tool that could change the imports across my package from absolute path to a relative path. Currently my import for package bar look like this :

import FOO_common/server/src/foo/bar

I want to convert this to

import foo/bar

Is there a tool like gofmt that could do this for me ? I know I can solve this with bash sed. However, am hoping for a go tool that might exist out there for the same.


Solution

  •  gofmt -w -r '"FOO_common/server/src/foo/bar" -> "foo/bar"' *.go
    

    This will just look for the strings as they are, and replace them with the new string.

    If you need more advanced functionality such as wildcards, you might want to look at this other tool:

    https://github.com/rogpeppe/govers