{"record":{"id":"4440619a3b699c49","repo":"inancgumus/learngo","slug":"please-provide-a-valid-number","errorCode":null,"errorMessage":"Please provide a valid number","messagePattern":"Please provide a valid number","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"x-tba/foundations/calc/08-funcs/main.go","lineNumber":54,"sourceCode":"\tif b, err = parse(os.Args[3]); err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\n\top := os.Args[2]\n\tres, err := calc(a, b, op)\n\tif err != nil {\n\t\tfmt.Println(err)\n\t\treturn\n\t}\n\n\tfmt.Printf(\"%v %s %v = %v\\n\", a, op, b, res)\n}\n\nfunc parse(snum string) (n float64, err error) {\n\tn, err = strconv.ParseFloat(snum, 64)\n\tif err != nil {\n\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 + \"'\")","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/inancgumus/learngo/blob/3c475a78e54336c6255e925ff66b8ef4da6a2ae7/x-tba/foundations/calc/08-funcs/main.go#L36-L72","documentation":"parse() wraps strconv.ParseFloat: if the operand string cannot be parsed as a float64, it discards the underlying error and returns the friendly message \"Please provide a valid number\". It exists to give calculator users a readable validation error instead of strconv's verbose syntax errors.","triggerScenarios":"Running the calc program with an operand argument that is not a number, e.g. calc 3 + x, calc ten * 2, or an empty operand string.","commonSituations":"Typos on the command line, accidentally passing the operator in an operand slot, shells mangling arguments (e.g. '*' globbing), or locale-formatted numbers like \"3,14\".","solutions":["Pass only valid numeric literals as the two operand arguments: calc 3.5 + 2.","Quote operators to prevent shell globbing/expansion: calc 3 \"*\" 2.","If the wrapped ParseFloat error is needed for debugging, wrap it instead of replacing it: fmt.Errorf(\"Please provide a valid number: %w\", err).","Use '.' as the decimal separator; ParseFloat does not accept comma decimals.","Handle negative numbers as \"-5\" (accepted) but note hex/exponents like 0x10 or 1e2 behave per ParseFloat rules."],"exampleFix":"// before\nerr = errors.New(\"Please provide a valid number\")\n// after\nerr = fmt.Errorf(\"Please provide a valid number: %w\", err)","handlingStrategy":"validation","validationCode":"n, err := strconv.ParseFloat(arg, 64)\nif err != nil {\n    fmt.Fprintf(os.Stderr, \"Please provide a valid number: %s\\n\", arg)\n    os.Exit(1)\n}","typeGuard":"func isNumber(s string) bool {\n    _, err := strconv.ParseFloat(s, 64)\n    return err == nil\n}","tryCatchPattern":"v, err := parse(arg)\nif err != nil {\n    if err.Error() == \"Please provide a valid number\" {\n        fmt.Fprintf(os.Stderr, \"usage: calc <num> <op> <num>\\n\")\n        os.Exit(2)\n    }\n    panic(err)\n}","preventionTips":["Validate argv operands with strconv.ParseFloat before use","Quote operators in the shell to keep argument order intact","Print usage on argument errors","Support %w-wrapping to keep the root ParseFloat cause"],"tags":["cli","validation","parsing","strconv"],"backgroundTag":"invalid-number-format","analyzedSha":"3c475a78e54336c6255e925ff66b8ef4da6a2ae7","analyzedAt":"2026-09-02T10:14:38.492Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}