Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions go/study/ch02_first_cli_app/calc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"
"strconv"
)

type Calculation interface {
Do(x, y int) Result
Kind() string
}

type Result interface {
Outcome() string
}

type Int int

func (i Int) Outcome() string {
return strconv.Itoa(int(i))
}

type Addition struct{}

func (*Addition) Kind() string {
return "addition"
}

func (*Addition) Do(x, y int) Result {
return Int(x + y)
}

type Subtraction struct{}

func (*Subtraction) Kind() string {
return "subtraction"
}

func (*Subtraction) Do(x, y int) Result {
return Int(x - y)
}

type Multiplication struct{}

func (*Multiplication) Kind() string {
return "multiplication"
}

func (*Multiplication) Do(x, y int) Result {
return Int(x * y)
}

type DivisionResult struct {
Quotient int
Remainder int
}

func (d *DivisionResult) Outcome() string {
return fmt.Sprintf("(quotient: %d, remainder: %d)", d.Quotient, d.Remainder)
}

type InvalidDenominator struct{}

func (i *InvalidDenominator) Outcome() string {
return "Invalid denominator. It must be not zero"
}

type Division struct{}

func (d *Division) Kind() string {
return "division"
}

func (d *Division) Do(x, y int) Result {
if y == 0 {
return &InvalidDenominator{}
}
q, r := x/y, x%y
return &DivisionResult{q, r}
}
48 changes: 48 additions & 0 deletions go/study/ch02_first_cli_app/calc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import "testing"

func TestAddtion(t *testing.T) {
calc := &Addition{}
if calc.Kind() != "addition" {
t.FailNow()
}
if calc.Do(1, 2) != Int(3) {
t.FailNow()
}
}

func TestSubtraction(t *testing.T) {
calc := &Subtraction{}
if calc.Kind() != "subtraction" {
t.FailNow()
}
if calc.Do(2, 1) != Int(1) {
t.FailNow()
}
}

func TestMultiplication(t *testing.T) {
calc := &Multiplication{}
if calc.Kind() != "multiplication" {
t.FailNow()
}
if calc.Do(2, 3) != Int(6) {
t.FailNow()
}
}

func TestDivision(t *testing.T) {
calc := &Division{}
if calc.Kind() != "division" {
t.FailNow()
}
if _, ok := calc.Do(2, 0).(*InvalidDenominator); !ok {
t.FailNow()
}
if r, ok := calc.Do(10, 3).(*DivisionResult); ok {
if r.Quotient != 3 || r.Remainder != 1 {
t.FailNow()
}
}
}
3 changes: 3 additions & 0 deletions go/study/ch02_first_cli_app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/tecyokomichi/WebAppLearning/go/study/ch02_first_cli_app

go 1.19
54 changes: 54 additions & 0 deletions go/study/ch02_first_cli_app/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"fmt"
"os"
"strconv"
)

var calcMap = map[string]Calculation{
"add": &Addition{},
"subtract": &Subtraction{},
"multiply": &Multiplication{},
"divide": &Division{},
}

func main() {
if len(os.Args) < 4 || 4 < len(os.Args) {
showHelp()
os.Exit(1)
} else if len(os.Args) == 4 {
calc, ok := calcMap[os.Args[1]]
if !ok {
showHelp()
os.Exit(1)
}
x, e := parseInt(os.Args[2])
if e != nil {
fmt.Printf("%s\n", e.Error())
os.Exit(1)
}
y, e := parseInt(os.Args[3])
if e != nil {
fmt.Printf("%s\n", e.Error())
os.Exit(1)
}
fmt.Printf("%s: %s\n", calc.Kind(), calc.Do(x, y).Outcome())
os.Exit(0)
}
}

func showHelp() {
fmt.Println("USAGE:")
fmt.Println("./ch02_first_cli (add|subtract|multiply|divide) X Y")
fmt.Println(" Shows the result of calculation with X and Y")
fmt.Println(" X and Y must be number")
}

func parseInt(s string) (int, error) {
r, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("ERROR: %q is not a number", s)
}
return r, nil
}