Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: functions Split and Base are not the same on windows and linux #7698

Closed
gopherbot opened this issue Apr 3, 2014 · 2 comments
Closed

Comments

@gopherbot
Copy link

by Gavin.Bong:

What does 'go version' print?

go version go1.2.1 windows/amd64

What steps reproduce the problem?
If possible, include a link to a program on play.golang.org.

1. This code runs on ubuntu 12.04 http://play.golang.org/p/40d_VGdt3Y
   and demonstrates that both path.Split and path.Base correctly returns
   the filename at the end of the path.

2. The following code (which can only work on windows because only windows has the TEMP
environment variable). I use %TEMP% because it gives a canonical path on windows OS.

import "path"
import "path/filepath"
import "fmt"
import "os"

func main() {
   fmt.Println("%TEMP% = ", os.Getenv("TEMP"))
   filename := os.Getenv("TEMP") + string(os.PathSeparator) + "video.mp4"

   d, f := path.Split(filename)
   base := path.Base(filename)

   fmt.Println("f=", f)
   fmt.Println("d=", d)
   fmt.Println(base)
}

What happened?

The execution produced this output.

1) %TEMP% = C:\Users\gavin\AppData\Local\Temp
2) f=C:\Users\gavin\AppData\Local\Temp\video.mp4
3) d=
4) C:\Users\gavin\AppData\Local\Temp\video.mp4


What should have happened instead?

1) Correct
2) f=video.mp4
3) d=C:\Users\gavin\AppData\Local\Temp
4) video.mp4

Please provide any additional information below.
@cznic
Copy link
Contributor

cznic commented Apr 3, 2014

Comment 1:

The program wrongly mixes use of cross platform os.PathSeparator and cross platform
filepath.Join and non cross platform path package. The later works only with slash
separated paths as documented:
"Package path implements utility routines for manipulating slash-separated paths."[0]
But os.PathSeparator is not a slash on windows.
  [0]: http://golang.org/pkg/path/
#WAI

@ianlancetaylor
Copy link
Contributor

Comment 2:

The path package is for slash-separated paths, like URLs.
The path/filepath package is for operating system specific path names.
If you want to use os.PathSeparator--an operating system specific concept--then use
path/filepath, not path.

Status changed to WorkingAsIntended.

penberg added a commit to cloudius-systems/capstan that referenced this issue Dec 16, 2015
Use filepath.Base() as it's portable across systems and not the UNIX
specific path.Base():

  golang/go#7698

Signed-off-by: Pekka Enberg <penberg@scylladb.com>
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants