duanguoping2016
2018-02-21 12:27
浏览 22
已采纳

在不同包中使用路径的便捷方式

I have a program in which I use a lot "../" which is to go one level up in the file system and run some process on the directory with specific name. I have a command line tool in Go.

I have 3 questions

  1. there is nicer way to do it instead of “../“
  2. is there a const with which I can use instead of “/“
  3. 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...)

example

   dir.zip("../"+tmpDirName, "../"+m.Id+".zip", "../"+tmpDirName)

图片转代码服务由CSDN问答提供 功能建议

我有一个程序在其中使用了很多“ ../”,可以将其向上一级 in 文件系统,并使用特定名称在目录上运行某些进程。 我在Go中有一个命令行工具。

我有3个问题

  1. 还有更好的方法 代替“ ../”
  2. 来执行它吗?如果我不能创建2,则可以使用const代替“ /”
  3. 。 内部包下的“常量”以在包之间共享“ /”,因为我在很多地方都需要它(来自diff包...) \ n

    示例

      dir.zip(“ ../"+ tmpDirName,” ../"+m.Id+".zip“,” ../“  + tmpDirName)
       
     
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhaxian1267 2018-02-21 12:36
    已采纳
    1. Set a variable, and use that everywhere:

      path := "../"
      

      or

      path := ".." + string(os.PathSeparator)
      

      then later:

      dir.zip(path+tmpDirName, path+m.Id+".zip", path+tmpDirName)
      

      This makes it very easy to change the path in the future, via a command line option, configuration, or just editing the value.

    2. Yes. os.PathSeparator is the OS-specific path separator for the current architecture.

    3. n/a
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsour68888 2018-02-23 13:45
    1. declare a global const somewhere, but I would just use ".." everywhere
    2. os.PathSeparator
    3. use filepath.Join("..", someDir, someFilename)
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题