join paths with backslash separator independent of the underlying os with the standard library

Viewed 620

filepath.Join allows to join paths by using the path separator of the underlying os (basically \ on Windows, / everywhere else).

Is there a way to force using the Windows path separator \ for joining paths with the Go standard library no matter what the actual running operating system is?

1 Answers

No, not with the standard lib's path/filepath package. You have to use strings.Join() instead.

If you do rely on "side-effects" of filepath.Join() (e.g. adding a separator if necessary, calling Clean() etc.), then easiest would be to call filepath.Join(), then replace slashes with backslashes, e.g. using strings.ReplaceAll().

Note that the replace-all method works because slash / is a reserved character and cannot appear in names other than separating folders and files. For details, see Naming Files, Paths, and Namespaces.

See related filepath.Join removes dot.

Related