import paths - slash vs backslash

157 views
Skip to first unread message

Tim Hockin

unread,
Nov 10, 2017, 1:31:37 PM11/10/17
to golang-nuts
Is it valid to say `import "github.com\foo\bar" (windows path
separators) or must it be "github.com/foo/bar"? I couldn't find a
canonical answer to this.

Thanks

Tim

Dave Cheney

unread,
Nov 10, 2017, 2:52:08 PM11/10/17
to golang-nuts
That's a good question.

The contents of the import declaration are defined to be implementation dependent by the spec

The interpretation of the ImportPath is implementation-dependent but it is typically a substring of the full file name of the compiled package and may be relative to a repository of installed packages.

It also goes on to say

Implementation restriction: A compiler may restrict ImportPaths to non-empty strings using only characters belonging to Unicode's L, M, N, P, and S general categories (the Graphic characters without spaces) and may also exclude the characters !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement character U+FFFD.

Suggesting that some implementations may exclude backslash.

In practice gc and gccgo treat the import path as a directory path, and while windows can cope with forward slashes, non windows operating systems cannot cope with backslash. So for simplicities sake, i'd say you should treat the import path like a path in a url--forward slashes only.

Tim Hockin

unread,
Feb 20, 2018, 12:16:14 AM2/20/18
to golang-nuts
Pinging this topic - does anyone know?

Ian Lance Taylor

unread,
Feb 20, 2018, 1:48:05 AM2/20/18
to Tim Hockin, golang-nuts
The language spec says that an implementation is permitted but not
required to forbid the backslash character in an import path
(https://golang.org/ref/spec#Import_declarations). The gc compiler
does in fact refuse to accept a backslash character in an import path.
So in practice you need to use forward slashes.

Ian

Volker Dobler

unread,
Feb 20, 2018, 3:15:14 AM2/20/18
to golang-nuts
Using backslashes as a path separator is wrong on Windows too.
Just keep it sane and use /.

V. 
Reply all
Reply to author
Forward
0 new messages