title: golang: cross platform path.Dir
body: <p>I'd like to use <code>path.Dir()</code> on Unix and Windows with a platform specific directory. Please take a look at the code:</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "path"
  )
  
  func main() {
      fmt.Println(`path.Dir("a/b/c"): `, path.Dir("a/b/c"))
      fmt.Println(`path.Dir("c:\foo\bar.exe"): `, path.Dir(`c:\foo\bar.exe`))
  }
  </code></pre>
  
  <p>This outputs </p>
  
  <pre><code>path.Dir("a/b/c"):  a/b
  path.Dir("c:\foo\bar.exe"):  .
  </code></pre>
  
  <p>I'd like to get for the second call to <code>path.Dir()</code> (windows) something like</p>
  
  <pre><code>c:\foo
  </code></pre>
  
  <p>Is it possible to tell <code>path.dir()</code> to use Windows separators for my program running on windows? Or should I always convert the backslashes <code>\</code>  to forward slashes (<code>/</code>)? What is the preferred strategy here?</p>