修复go imports,将绝对路径改为相对路径。

huangapple go评论82阅读模式
英文:

Fixing go imports to relative instead of absolute path

问题

有没有一种工具可以将我的包中的导入从绝对路径更改为相对路径。目前我的package bar的导入看起来像这样:

import FOO_common/server/src/foo/bar

我想将其转换为:

import foo/bar

是否有像gofmt这样的工具可以为我完成这个操作?我知道我可以使用bash的sed来解决这个问题。但是,我希望有一个可能存在的go tool可以做到这一点。

英文:

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.

答案1

得分: 2

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

这将仅查找字符串,并用新字符串替换它们。

如果你需要更高级的功能,比如通配符,你可能想看看这个工具:

https://github.com/rogpeppe/govers

英文:
 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

huangapple
  • 本文由 发表于 2017年6月28日 02:31:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/44787465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定