inancgumus/learngo · error

wrong input: %v (line #%d)

Error message

wrong input: %v (line #%d)

What it means

The v4 parser is a stateful type: Parse increments p.lines, splits each line, and requires exactly 2 fields. On a mismatch it stores 'wrong input: %v (line #%d)' in p.lerr (instead of returning an error) and returns early; the caller checks p.lerr after the loop.

Source

Thrown at logparser/v4/parser.go:48

	lerr    error             // the last error occurred
}

// 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) (r result) {
	if p.lerr != nil {
		return
	}

	p.lines++

	fields := strings.Fields(line)
	if len(fields) != 2 {
		p.lerr = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
		return
	}

	var err error

	r.domain = fields[0]
	r.visits, err = strconv.Atoi(fields[1])

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

// update updates all the parsing results using the given parsing result
func update(p *parser, r result) {
	if p.lerr != nil {
		return

View on GitHub (pinned to 3c475a78e5)

Solutions

  1. Skip blank/whitespace-only lines inside Parse before counting them
  2. Validate the file format before parsing (e.g. check the first few lines)
  3. Check p.lerr after parsing and report it with context

Example fix

// before
p.lines++
fields := strings.Fields(line)
if len(fields) != 2 {
    p.lerr = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
    return
}
// after
if strings.TrimSpace(line) == "" {
    return // ignore blank lines
}
p.lines++
fields := strings.Fields(line)
if len(fields) != 2 {
    p.lerr = fmt.Errorf("wrong input on line #%d: %q", p.lines, line)
    return
}
Defensive patterns

Strategy: validation

Validate before calling

func validV4Line(line string) bool {
    if strings.TrimSpace(line) == "" { return false }
    return len(strings.Fields(line)) == 2
}
// only call p.Parse(line) when validV4Line(line)

Type guard

func isParsable(line string) bool {
    fs := strings.Fields(line)
    return len(fs) == 2 && func() bool { _, e := strconv.Atoi(fs[1]); return e == nil }()
}

Try / catch

if err := p.Parse(f); err != nil {
    return err
}
if p.lerr != nil {
    log.Printf("stopped at %v", p.lerr)
}

Prevention

When it happens

Trigger: Calling Parse on a line that doesn't split into exactly 'domain visits' — wrong field count including empty lines.

Common situations: Files mixing formats, blank separator lines between sections, legacy 3-field lines, or trailing blank line at EOF treated as a record.

Related errors


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