inancgumus/learngo · warning

Wrong operation: '<op>'

Error message

Wrong operation: '<op>'

What it means

calc()'s switch handles +,-,*,/,% and their word aliases (plus, div, mod, etc.); any other operator string hits the default case and returns this error. The error echoes the unsupported operator so users can see what was rejected.

Source

Thrown at x-tba/foundations/calc/08-funcs/main.go:72

		err = errors.New("Please provide a valid number")
	}
	return
}

func calc(a, b float64, op string) (res float64, err error) {
	switch op {
	case "+", "plus":
		op, res = "+", a+b
	case "-", "minus":
		op, res = "-", a-b
	case "*", "times":
		op, res = "*", a*b
	case "/", "div":
		op, res = "/", a/b
	case "%", "mod":
		res = float64(int(a) % int(b))
	default:
		return 0, errors.New("Wrong operation: '" + op + "'")
	}
	return
}

View on GitHub (pinned to 3c475a78e5)

Solutions

  1. Use one of the supported operators (+ - * / % or their word aliases) and quote shell metacharacters: calc 3 "*" 2.
  2. Check the program's help/source (main.go) for the exact accepted aliases.
  3. If ^ or ** is needed, add a case for it in calc()'s switch (e.g. math.Pow for ^).
  4. Print the supported operator list in usage output to prevent confusion.

Example fix

// before
case "%", "mod":
    res = float64(int(a) % int(b))
default:
    return 0, errors.New("Wrong operation: '" + op + "'")
// after
case "%", "mod":
    res = float64(int(a) % int(b))
case "^", "pow":
    res = math.Pow(a, b)
default:
    return 0, fmt.Errorf("Wrong operation: %q (supported: + - * / %% ^)", op)
Defensive patterns

Strategy: validation

Validate before calling

var validOps = map[string]bool{"+":true,"-":true,"*":true,"/":true,"%":true,"plus":true,"div":true,"mod":true}
if !validOps[op] {
    fmt.Fprintf(os.Stderr, "unsupported operator: %s\n", op)
    os.Exit(2)
}

Type guard

func isSupportedOp(op string) bool {
    switch op {
    case "+", "-", "*", "/", "%", "plus", "minus", "mul", "div", "mod":
        return true
    }
    return false
}

Try / catch

res, err := calc(a, b, op)
if err != nil {
    if strings.HasPrefix(err.Error(), "Wrong operation:") {
        fmt.Fprintf(os.Stderr, "%v (supported: + - * / %%)\n", err)
        os.Exit(2)
    }
    panic(err)
}

Prevention

When it happens

Trigger: Passing an operator argument outside the supported set, e.g. calc 3 ** 2, calc 3 ^ 2, calc 5 // 2, or a misspelled word like 'subtrakt'.

Common situations: Assuming exponent or integer-division operators exist; shell mangling of * into a file list; wrong argument order putting a number in the operator slot; language-locale operator names.

Related errors


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