golang/go · error
import path contains invalid UTF-8 sequence: %q
Error message
import path contains invalid UTF-8 sequence: %q
What it means
Returned by checkImportPath when iterating the path's runes encounters utf8.RuneError, which signals invalid UTF-8 (either a lone continuation byte or an overlong/invalid sequence). Go import paths must be valid UTF-8.
Source
Thrown at src/cmd/compile/internal/noder/import.go:320
func checkImportPath(path string, allowSpace bool) error {
if path == "" {
return errors.New("import path is empty")
}
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
- Re-encode the import path as UTF-8 (iconv or your editor's encoding setting).
- Retype the import line by hand to drop stray bytes.
- Validate with a UTF-8 checker (e.g. printf %s path | iconv -f utf-8 -t utf-8).
Defensive patterns
Strategy: validation
Validate before calling
if !utf8.ValidString(path) {
return fmt.Errorf("import path is not valid UTF-8: %q", path)
} Prevention
- Generate import paths from ASCII identifiers.
- Validate paths with utf8.ValidString before compiling.
When it happens
Trigger: An import path string contains bytes that do not decode as valid UTF-8; the for-range loop yields utf8.RuneError and the error names the whole path.
Common situations: Pasting an import path with mojibake or non-UTF-8 bytes; a filesystem-derived path encoded in Latin-1 or GBK; binary corruption of a source file's import line.
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
- import path %q is reserved and cannot be used
- import path contains control character: %q
- import path contains backslash; use slash: %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/f9ea2b9ef9e1ca3a.
Report an issue: GitHub.