inancgumus/learngo · error

wrong input: %v (line #%d)

Error message

wrong input: %v (line #%d)

What it means

In the v3 parser, parse accepts lines of exactly 2 fields (domain and visits). Any other field count sets the returned error 'wrong input: %v (line #%d)', embedding the parsed fields slice and the current line number p.lines, and returns a zero result.

Source

Thrown at logparser/v3/parser.go:41

// parser keep tracks of the parsing
type parser struct {
	sum     map[string]result // metrics per domain
	domains []string          // unique domain names
	total   int               // total visits for all domains
	lines   int               // number of parsed lines (for the error messages)
}

// 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

View on GitHub (pinned to 3c475a78e5)

Solutions

  1. Ensure the input file uses the 2-field 'domain visits' format throughout
  2. Filter or convert 3-field legacy lines before parsing
  3. Improve the message to state the expected format explicitly

Example fix

// before
if len(fields) != 2 {
    err = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
    return
}
// after
if len(fields) != 2 {
    err = fmt.Errorf("wrong input on line #%d: expected 'domain visits', got %q", p.lines, line)
    return
}
Defensive patterns

Strategy: validation

Validate before calling

func validVisitLine(line string) bool {
    fs := strings.Fields(line)
    return len(fs) == 2
}
// filter lines before parse(p, line)

Type guard

func hasTwoFields(line string) bool {
    return len(strings.Fields(line)) == 2
}

Try / catch

parsed, err := parse(p, line)
if err != nil {
    if strings.Contains(err.Error(), "wrong input") {
        log.Printf("skipping: %v", err)
        continue
    }
    return err
}

Prevention

When it happens

Trigger: Calling parse (via main's per-line loop) with a line that splits into 0, 1, or 3+ tokens — e.g. a domain-only line, or a line with domain visits plus an extra column.

Common situations: Mixed log versions where some lines still carry a third column (e.g. time spent from the older 3-field format), blank lines, or extra whitespace-separated annotations.

Related errors


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