-3

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.

nitimalh
  • 919
  • 10
  • 26
  • `foo/bar` is not a relative path - Go assumes that imports are rooted in `GOPATH/src`. A relative path would be "./foo/bar", but that's likely not what you want, and you would have to use a different import statement depending on where each file is in your hierarchy. You probably want to stick with normal imports. – Adrian Jun 27 '17 at 18:33
  • 1
    You really do not want to use relative import paths. They are not supported in all cases by the Go tooling, and just meant for quick, on-off tests. – JimB Jun 27 '17 at 18:34
  • @Adrian My mistake ... I did mean `$GOPATH/src/foo/bar` provided `$GOPATH` points to my project directory – nitimalh Jun 27 '17 at 18:37
  • 1
    Then that's not a relative path - as far as Go is concerned, that's a normal import path. – Adrian Jun 27 '17 at 18:40

1 Answers1

2
 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

eugenioy
  • 11,825
  • 28
  • 35