-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
os: TempDir() different between OSx and Linux #21318
Comments
What does
echo $TMPDIR
on both systems report?
… On 5 Aug 2017, at 15:04, mrfly ***@***.***> wrote:
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version)?
go version 1.8.3
What operating system and processor architecture are you using (go env)?
Linux and mac OS
What did you do?
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println(os.TempDir())
}
What did you expect to see?
Linux and MacOS has the same output, which they both has no slash in the end.
What did you see instead?
go in OSX has a slash in fmt.Println(os.TempDir())
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Change https://golang.org/cl/53412 mentions this issue: |
hello @davecheney But if If you just want to get the temp directory, that's fine, but once you want to get the file name you created in the temp directory, and trim the temp dir, it will be different. https://play.golang.org/p/yz4DLEPoNr package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
file, _ := ioutil.TempFile(os.TempDir(), "file-")
fp := strings.TrimPrefix(file.Name(), os.TempDir())
fmt.Println(file.Name())
fmt.Println(fp)
} In the Unix systems, you'll get:
But in OS X:
I got a same A, but a different B. |
Tmpdir is what the operating system says it is.
Also, why does it matter if there is a trailing slash?
… On 5 Aug 2017, at 18:25, mrfly ***@***.***> wrote:
@davecheney The shell knows nothing...
But if fmt.Println(os.TempDir()), OS X will tell you /var/folders/t9/uaiyebqwpojxbaywgebzmx/T/, while the Unix told you /tmp.
If you just want to get the temp directory, that's fine, but once if you want to get the file name you created in the temp directory, it will be different.
https://play.golang.org/p/yz4DLEPoNr
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func main() {
file, _ := ioutil.TempFile(os.TempDir(), "file-")
fp := strings.TrimPrefix(file.Name(), os.TempDir())
fmt.Println(file.Name())
fmt.Println(fp)
}
In the Unix systems, you'll get:
/tmp/file-054003078
/file-054003078
But in OS X:
/var/folders/t9/uaiyebqwpojxbaywgebzmx/T/file-054003078
file-054003078
I got a same A, but a different B.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
For serving a file: It's not good that a same program behave inconsistently on different systems. |
If you want to join paths, portably, use filepath.Join, not string
concatenation.
…On Sat, Aug 5, 2017 at 6:47 PM, mrfly ***@***.***> wrote:
For serving a file:
If the temp file name is /temp-file-in-temp-dir, and the server's path is
/var/www/files, I can combine it with df := fmt.Sprintf("%s%s",path,
filename) in Unix, but in OS X, it will be df := fmt.Sprintf("%s/%s",path,
filename) since the filename doesn't contains the first slash.
It's not good that a same program behave inconsistently on different
systems.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#21318 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAcAxlnkPrsa7wZBnr3XygBWZk_fvLwks5sVCwngaJpZM4OuXeg>
.
|
While I would also recommend filepath.Join, use always "%s/%s" with printf. UNIX treats "a//b" as "a/b" and therefore "%s/%s" works regardless whether the first path is terminated by a slash or not. Even if we would define that TempDir should always return a cleaned path as produced by filepath.Clean you couldn't assume that it never ends in a slash, because it could return the root path "/". |
That's a little embarrassed.... I've even created a code-review request in Gerrit.... BTW, is there any standard for return a directory's name? Thanks you guys. |
I think that is the definition that is relevant: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_266 A pathname may optionally contain one or more trailing slashes. All what TempDir is doing is returning a pathname. |
Got that, thank you very much! @ulikunitz @davecheney |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version 1.8.3
What operating system and processor architecture are you using (
go env
)?Linux and mac OS
What did you do?
What did you expect to see?
Linux and MacOS has the same output, which they both has no slash in the end.
What did you see instead?
go in OSX has a slash in
fmt.Println(os.TempDir())
The text was updated successfully, but these errors were encountered: