c:\foo
path.dir() \ /
path.Dir() / filepath.Dir()
package main
import(
fmt
path / filepath
)
func main(){
fmt.Println(`filepath.Dir(a / b / c):`,filepath.Dir(a / b / c))
fmt.Println(`filepath.Dir(c:\ foo \ bar.exe):`,filepath .dir(`c:\foo\bar.exe`))
}
在Windows上:
filepath.Dir(a / b / c):a \ b
filepath .Dir(c:\foo\bar.exe):c:\ foo
I'd like to use path.Dir() on Unix and Windows with a platform specific directory. Please take a look at the code:path.Dir()
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`))
}
This outputs
path.Dir("a/b/c"): a/b
path.Dir("c:\foo\bar.exe"): .
I'd like to get for the second call to path.Dir() (windows) something likepath.Dir()
c:\foo
Is it possible to tell path.dir() to use Windows separators for my program running on windows? Or should I always convert the backslashes \ to forward slashes (/)? What is the preferred strategy here?path.dir()\/
I see where the "problem" is. This discussion at golang-nuts gave me the hint, that path.Dir() always uses / and filepath.Dir() is the function to be used for platform dependent manipulation.path.Dir()/filepath.Dir()
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Println(`filepath.Dir("a/b/c"): `, filepath.Dir("a/b/c"))
fmt.Println(`filepath.Dir("c:\foo\bar.exe"): `, filepath.Dir(`c:\foo\bar.exe`))
}
on windows:
filepath.Dir("a/b/c"): a\b
filepath.Dir("c:\foo\bar.exe"): c:\foo
这篇关于golang:跨平台path.Dir的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!