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 declare

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Use only valid Go import path characters: letters, digits, slashes, dots, dashes, underscores, and tildes
  2. Fix the module path in go.mod if it contains illegal characters
  3. Check for invisible characters or smart quotes by examining the file with a hex editor or cat -A
  4. 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

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

Related errors


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