inancgumus/learngo · error

wrong input: %q (line #%d)

Error message

wrong input: %q (line #%d)

What it means

After splitting, v3's parse converts the second field with strconv.Atoi. If conversion fails or the visits value is negative, it replaces the error with 'wrong input: %q (line #%d)' quoting just the offending visits token plus the line number, and returns without a result.

Source

Thrown at logparser/v3/parser.go:49

// newParser constructs, initializes and returns a new parser
func newParser() parser {
	return parser{sum: make(map[string]result)}
}

// parse parses a log line and returns the parsed result with an error
func parse(p parser, line string) (parsed result, err error) {
	fields := strings.Fields(line)
	if len(fields) != 2 {
		err = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
		return
	}

	parsed.domain = fields[0]

	parsed.visits, err = strconv.Atoi(fields[1])
	if parsed.visits < 0 || err != nil {
		err = fmt.Errorf("wrong input: %q (line #%d)", fields[1], p.lines)
		return
	}

	return
}

// update updates the parser for the given parsing result
func update(p parser, parsed result) parser {
	domain, visits := parsed.domain, parsed.visits

	// Collect the unique domains
	if _, ok := p.sum[domain]; !ok {
		p.domains = append(p.domains, domain)
	}

	// Keep track of total and per domain visits
	p.total += visits

View on GitHub (pinned to 3c475a78e5)

Solutions

  1. Fix the negative/non-numeric visits value in the input
  2. Pre-validate with strconv.Atoi or a ^\d+$ regex before parse
  3. Decide whether negative counts should be rejected or clamped/warned

Example fix

// before
if parsed.visits < 0 || err != nil {
    err = fmt.Errorf("wrong input: %q (line #%d)", fields[1], p.lines)
    return
}
// after
if err != nil {
    err = fmt.Errorf("line #%d: visits %q is not a number", p.lines, fields[1])
    return
}
if parsed.visits < 0 {
    err = fmt.Errorf("line #%d: visits %d is negative", p.lines, parsed.visits)
    return
}
Defensive patterns

Strategy: validation

Validate before calling

var visitsRe = regexp.MustCompile(`^\d+$`)
fs := strings.Fields(line)
if len(fs) == 2 && !visitsRe.MatchString(fs[1]) {
    log.Printf("line invalid, skipping")
    continue
}

Type guard

func isValidVisits(s string) bool {
    n, err := strconv.Atoi(s)
    return err == nil && n >= 0
}

Try / catch

parsed, err := parse(p, line)
if err != nil {
    var ve *strconv.NumError
    if errors.As(err, &ve) {
        log.Printf("non-numeric visits on line %d: %v", p.lines, ve.Num)
        continue
    }
    return err
}

Prevention

When it happens

Trigger: A line like 'example.com -3' (negative) or 'example.com abc' (non-numeric) passed to parse; Atoi's error or the < 0 check triggers the replacement error.

Common situations: Signed counts from buggy upstream writers, typos ('l0' instead of '10'), values with separators ('1,000'), or placeholder values ('N/A') in exported logs.

Related errors


AI-assisted analysis of inancgumus/learngo@3c475a78e5 (2026-09-02). Data as JSON: /api/errors/bc8dc5f17950504f. Report an issue: GitHub.