Created
May 29, 2017 17:38
-
-
Save dlespiau/9f1b4fca65147a06ed7a9e59aadc73c3 to your computer and use it in GitHub Desktop.
calc - the awesome calculator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add is tested with a unit test | |
func add(a, b int) int { | |
return a + b | |
} | |
// sub is tested by running "calc sub 2 3". | |
func sub(a, b int) int { | |
return a - b | |
} | |
// The error paths in main are tested by invoking calc. | |
func main() { | |
if len(os.Args) != 4 { | |
fmt.Fprintf(os.Stderr, "expected 3 arguments, got %d\n", len(os.Args)-1) | |
exit.Exit(1) | |
} | |
a, _ := strconv.Atoi(os.Args[2]) | |
b, _ := strconv.Atoi(os.Args[3]) | |
var op func(int, int) int | |
switch os.Args[1] { | |
case "add": | |
op = add | |
case "sub": | |
op = sub | |
default: | |
fmt.Fprintf(os.Stderr, "unknown operation: %s\n", os.Args[1]) | |
exit.Exit(1) | |
} | |
fmt.Println(op(a, b)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment