英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论