Why does filepath.Join("C:", "Users") return "C:Users"?

on windows, filepath.Join(“C:”, “Users”) returns “C:Users” (go 1.7.4). This was surprising to me, since filepath.Join(“a”, “b”) on windows adds a backslash and returns a\b. Is this a feature or a bug?

My guess is it’s a feature, as C:Users is a valid path which is different from C:\Users.

You could say the same thing about UNC paths: \\machine\share\doc is a valid path distinct from \\machine\sharedoc, but filepath.Join(\\machine\share, doc) sensibly returns the former not the latter. Am I missing something?

Yes, in that case Join() would have joined together two different path elements into a single path element, which would be wrong. The docs say it adds a path separator when necessary. In your first example, it’s not necessary.

I guess the thing here is that on Windows, C: does not represent the root directory on C:, that’s C:\. The two are different things, and both are valid to use in Join() and the result will mean different things in turn.

This is correct, “C:” represents just the volume, and “C:Users” represents the item “Users” in the current directory of the C: volume. Here is a related discussion on the topic.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.