golang/go · error
import path contains backslash; use slash: %q
Error message
import path contains backslash; use slash: %q
What it means
Returned by checkImportPath when a path rune is a backslash. Go import paths use forward slashes as separators (like URL paths) and forbid backslashes, which is especially important on Windows where backslash is a path separator.
Source
Thrown at src/cmd/compile/internal/noder/import.go:324
if strings.Contains(path, "\x00") {
return errors.New("import path contains NUL")
}
for ri := range base.ReservedImports {
if path == ri {
return fmt.Errorf("import path %q is reserved and cannot be used", path)
}
}
for _, r := range path {
switch {
case r == utf8.RuneError:
return fmt.Errorf("import path contains invalid UTF-8 sequence: %q", path)
case r < 0x20 || r == 0x7f:
return fmt.Errorf("import path contains control character: %q", path)
case r == '\\':
return fmt.Errorf("import path contains backslash; use slash: %q", path)
case !allowSpace && unicode.IsSpace(r):
return fmt.Errorf("import path contains space character: %q", path)
case strings.ContainsRune("!\"#$%&'()*,:;<=>?[]^`{|}", r):
return fmt.Errorf("import path contains invalid character '%c': %q", r, path)
}
}
return nil
}
View on GitHub (pinned to b6b368adc5)
Solutions
- Replace all backslashes with forward slashes in the import path.
- Use the module path (example.com/foo) rather than an absolute filesystem path.
- Run gofmt/goimports, which normalize import paths.
Example fix
// before import "example\\foo" // after import "example.com/foo"
Defensive patterns
Strategy: validation
Validate before calling
if strings.ContainsRune(path, '\\') {
return fmt.Errorf("use forward slashes in import path: %q", path)
} Prevention
- Use module paths (example.com/foo), not OS filesystem paths.
- On Windows, normalize backslashes to slashes before importing.
When it happens
Trigger: An import path like example\\foo or C:\\Users\\... containing '\\'; the switch case r == '\\' fires.
Common situations: Windows developers pasting filesystem paths into imports instead of module paths; tools generating paths with OS separators; mixing URL-style and Windows-style separators.
Related errors
- import path %q is reserved and cannot be used
- import path contains invalid UTF-8 sequence: %q
- import path contains control character: %q
- import path contains space character: %q
- import path contains invalid character '%c': %q
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/126e78e06b4f52e5.
Report an issue: GitHub.