Skip to content

Instantly share code, notes, and snippets.

@dlespiau
Created May 29, 2017 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlespiau/9f1b4fca65147a06ed7a9e59aadc73c3 to your computer and use it in GitHub Desktop.
Save dlespiau/9f1b4fca65147a06ed7a9e59aadc73c3 to your computer and use it in GitHub Desktop.
calc - the awesome calculator
// 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