我有一个程序,我在其中使用了很多“../”,这是为了更上一层楼 在文件系统中并在具有特定名称的目录上运行一些进程。我在 Go 中有一个命令行工具。
我有3个问题
- there is nicer way to do it instead of “../“
- is there a const with which I can use instead of “/“
- if 2 is not available should I create “constants“ under that internal package to share the “/“ between packages since I need it in many place (from diff packages...)
例子
dir.zip("../"+tmpDirName, "../"+m.Id+".zip", "../"+tmpDirName)
最佳答案
设置一个变量,然后在任何地方使用它:
path := "../"
或
path := ".." + string(os.PathSeparator)
然后:
dir.zip(path+tmpDirName, path+m.Id+".zip", path+tmpDirName)
这使得将来通过命令行选项、配置或仅编辑值来更改路径变得非常容易。
是的。 os.PathSeparator是当前架构的特定于操作系统的路径分隔符。
- 不适用
关于go - 在不同包中使用路径的便捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48905939/