Simple Calculator Project
Module Objectives
Creating a Project Repository


Making a simple calculator

Writing Test Cases
Last updated



Last updated
package main
import "fmt"
func Add(x, y int) (res int) {
return x + y
}
func Subtract(x, y int) (res int) {
return x - y
}
func main() {
fmt.Println("Addition: ", Add(1, 2))
fmt.Println("Subtraction: ", Subtract(4, 2))
}Addition: 3
Subtraction: 2> git switch -c main
> git add .
> git commit -m "Calculator Demo"
> git push origin mainpackage main
import "fmt"
func Add(x, y int) (res int) {
return x + y
}
func Subtract(x, y int) (res int) {
return x - y
}
func main() {
fmt.Println("Addition: ", Add(1, 2))
fmt.Println("Subtraction: ", Subtract(4, 2))
}package main
import "testing"
func TestAdd(t *testing.T){
got := Add(4, 4)
want:= 8
if got != want {
t.Errorf("got %q. wanted %q", got, want)
}
}
func TestSubtract(t *testing.T){
got := Subtract(6, 4)
want:= 2
if got != want {
t.Errorf("got %q. wanted %q", got, want)
}
}
> go mod init calculator
> go test
# Result
> go test
PASS
ok calculator 0.127s> go test
--- FAIL: TestAdd (0.00s)
calculator_test.go:9: got '\b'. wanted '\x05'
--- FAIL: TestSubtract (0.00s)
calculator_test.go:19: got '\x02'. wanted '\x03'
FAIL
exit status 1
FAIL calculator 0.215s