golang/go · error
invalid character %#U
Error message
invalid character %#U
What it means
After unquoting and checking for emptiness, validatedImportPath scans each rune of the import path against a set of illegal characters (!"#$%&'()*,:;<=>?[\]^{|}` plus U+FFFD) and Unicode categories (non-graphic, whitespace). If any rune matches, this error is returned with the offending character in %#U format (e.g., invalid character U+0021 '!'). The function returns the partial path (up to the error) as the first return value.
Source
Thrown at src/cmd/compile/internal/types2/resolver.go:84
}
case l > r && (constDecl || r != 1): // if r == 1 it may be a multi-valued function and we can't say anything yet
n := names[r]
check.errorf(n, code, "missing init expr for %s", n.Value)
}
}
func validatedImportPath(path string) (string, error) {
s, err := strconv.Unquote(path)
if err != nil {
return "", err
}
if s == "" {
return "", fmt.Errorf("empty string")
}
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
for _, r := range s {
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return s, fmt.Errorf("invalid character %#U", r)
}
}
return s, nil
}
// declarePkgObj declares obj in the package scope, records its ident -> obj mapping,
// and updates check.objMap. The object must not be a function or method.
func (check *Checker) declarePkgObj(ident *syntax.Name, obj Object, d *declInfo) {
assert(ident.Value == obj.Name())
// spec: "A package-scope or file-scope identifier with name init
// may only be declared to be a function with this (func()) signature."
if ident.Value == "init" {
check.error(ident, InvalidInitDecl, "cannot declare init - must be func")
return
}
// spec: "The main package must have package name main and declareView on GitHub (pinned to b6b368adc5)
Solutions
- Use only valid Go import path characters: letters, digits, slashes, dots, dashes, underscores, and tildes
- Fix the module path in go.mod if it contains illegal characters
- Check for invisible characters or smart quotes by examining the file with a hex editor or cat -A
- Run gofmt on the source file — it may flag or normalize some import path issues
Example fix
// before import "example.com/foo;bar" // after import "example.com/foo/bar"
Defensive patterns
Strategy: validation
Validate before calling
// Validate import paths contain only legal characters
func validateImportPath(path string) error {
if path == "" {
return fmt.Errorf("empty import path")
}
const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
for _, r := range path {
if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
return fmt.Errorf("invalid character %#U in import path %q", r, path)
}
}
return nil
} Prevention
- Use only ASCII letters, digits, slashes, dots, dashes, and underscores in import paths
- Avoid URL-style prefixes (https://, git+) in import paths — use the module path directly
- Check for invisible characters in import paths when copy-pasting from documentation
- Run go vet which can detect some import path issues
When it happens
Trigger: An import path containing any character from the illegalChars set, non-graphic Unicode characters, or whitespace. Examples: import "example.com/foo;bar" (semicolon), import "example.com/foo bar" (space), import "example.com/foo\x00bar" (null byte), import containing Unicode replacement character U+FFFD.
Common situations: Typos in import paths. Shell escaping issues that inject special characters. URL-style paths accidentally used as import paths (e.g., https://). Copy-paste from rich text that includes smart quotes or invisible characters. Module paths with unusual characters.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- empty string
- Config.Importer not installed
- Config.Importer.ImportFrom(%s, %s, 0) returned nil but no er
- ErrHeader
- ErrWriteTooLong
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/5d1f505b018fd680.
Report an issue: GitHub.