{"record":{"id":"324ed334970ef541","repo":"inancgumus/learngo","slug":"wrong-operation-op","errorCode":null,"errorMessage":"Wrong operation: '<op>'","messagePattern":"Wrong operation: '<op>'","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"x-tba/foundations/calc/08-funcs/main.go","lineNumber":72,"sourceCode":"\t\terr = errors.New(\"Please provide a valid number\")\n\t}\n\treturn\n}\n\nfunc calc(a, b float64, op string) (res float64, err error) {\n\tswitch op {\n\tcase \"+\", \"plus\":\n\t\top, res = \"+\", a+b\n\tcase \"-\", \"minus\":\n\t\top, res = \"-\", a-b\n\tcase \"*\", \"times\":\n\t\top, res = \"*\", a*b\n\tcase \"/\", \"div\":\n\t\top, res = \"/\", a/b\n\tcase \"%\", \"mod\":\n\t\tres = float64(int(a) % int(b))\n\tdefault:\n\t\treturn 0, errors.New(\"Wrong operation: '\" + op + \"'\")\n\t}\n\treturn\n}\n","sourceCodeStart":54,"sourceCodeEnd":76,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/x-tba/foundations/calc/08-funcs/main.go#L54-L76","documentation":"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.","triggerScenarios":"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'.","commonSituations":"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.","solutions":["Use one of the supported operators (+ - * / % or their word aliases) and quote shell metacharacters: calc 3 \"*\" 2.","Check the program's help/source (main.go) for the exact accepted aliases.","If ^ or ** is needed, add a case for it in calc()'s switch (e.g. math.Pow for ^).","Print the supported operator list in usage output to prevent confusion."],"exampleFix":"// before\ncase \"%\", \"mod\":\n    res = float64(int(a) % int(b))\ndefault:\n    return 0, errors.New(\"Wrong operation: '\" + op + \"'\")\n// after\ncase \"%\", \"mod\":\n    res = float64(int(a) % int(b))\ncase \"^\", \"pow\":\n    res = math.Pow(a, b)\ndefault:\n    return 0, fmt.Errorf(\"Wrong operation: %q (supported: + - * / %% ^)\", op)","handlingStrategy":"validation","validationCode":"var validOps = map[string]bool{\"+\":true,\"-\":true,\"*\":true,\"/\":true,\"%\":true,\"plus\":true,\"div\":true,\"mod\":true}\nif !validOps[op] {\n    fmt.Fprintf(os.Stderr, \"unsupported operator: %s\\n\", op)\n    os.Exit(2)\n}","typeGuard":"func isSupportedOp(op string) bool {\n    switch op {\n    case \"+\", \"-\", \"*\", \"/\", \"%\", \"plus\", \"minus\", \"mul\", \"div\", \"mod\":\n        return true\n    }\n    return false\n}","tryCatchPattern":"res, err := calc(a, b, op)\nif err != nil {\n    if strings.HasPrefix(err.Error(), \"Wrong operation:\") {\n        fmt.Fprintf(os.Stderr, \"%v (supported: + - * / %%)\\n\", err)\n        os.Exit(2)\n    }\n    panic(err)\n}","preventionTips":["Quote * and other metacharacters in the shell: calc 3 \"*\" 2","List supported operators in --help output","Validate the operator argument before calling calc","Normalize operator aliases in one lookup table"],"tags":["cli","validation","argument-parsing"],"backgroundTag":"unsupported-operator","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}