golang/go · error

import path contains invalid character '%c': %q

Error message

import path contains invalid character '%c': %q

What it means

Returned by checkImportPath when a path rune is one of the explicitly disallowed punctuation characters (!\"#$%&'()*,:;<=>?[]^`{|}). These characters conflict with Go syntax, shell metacharacters, go.mod syntax, or URL/path semantics, so import paths reject them.

Source

Thrown at src/cmd/compile/internal/noder/import.go:328

	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. Strip or replace the offending punctuation in the import path.
  2. Use only letters, digits, dots, dashes, underscores, and slashes (plus tilde for version selectors handled elsewhere).
  3. Regenerate the import path from sanitized identifiers.

Example fix

// before
 import "example.com/foo:bar"
// after
 import "example.com/foobar"
Defensive patterns

Strategy: validation

Validate before calling

const bad = "!\"#$%&'()*,:;<=>?[]^`{|}"
for _, r := range path {
    if strings.ContainsRune(bad, r) {
        return fmt.Errorf("invalid character %q in import path", r)
    }
}

Prevention

When it happens

Trigger: An import path contains any of ! \" # $ % & ' ( ) * , : ; < = > ? [ ] ^ ` { | }; strings.ContainsRune matches and the error names both the character and the full path.

Common situations: Pasting a URL-like path with a colon or query string; using a path with brackets for generics-style decoration; shell glob characters leaking into a generated path; quotes around an import literal.

Understand the failure class

Related errors


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