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

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)
  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
  1. declare a global const somewhere, but I would just use ".." everywhere
  2. os.PathSeparator
  3. use filepath.Join("..", someDir, someFilename)