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

  1. Replace all backslashes with forward slashes in the import path.
  2. Use the module path (example.com/foo) rather than an absolute filesystem path.
  3. 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

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


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/126e78e06b4f52e5. Report an issue: GitHub.