From ea49f6ef972a37b4ca25129cf1f6c8f0f4bc074d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 12:53:12 +0900 Subject: [PATCH 01/16] initial commit --- go/study/ch02_first_cli_app/go.mod | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 go/study/ch02_first_cli_app/go.mod diff --git a/go/study/ch02_first_cli_app/go.mod b/go/study/ch02_first_cli_app/go.mod new file mode 100644 index 00000000..b8982bd0 --- /dev/null +++ b/go/study/ch02_first_cli_app/go.mod @@ -0,0 +1,3 @@ +module github.com/tecyokomichi/WebAppLearning/go/study/ch02_first_cli_app + +go 1.19 From d4cc2344b777a7f0efd741090b6f15c1706cf729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 13:11:02 +0900 Subject: [PATCH 02/16] =?UTF-8?q?:+1:=20main.go=20=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch02_first_cli_app/main.go | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 go/study/ch02_first_cli_app/main.go diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go new file mode 100644 index 00000000..16e2b22a --- /dev/null +++ b/go/study/ch02_first_cli_app/main.go @@ -0,0 +1,5 @@ +package main + +func main() { + println("Hello, World!") +} From 248546032427d3be8f2590ec9e8db1a313b74c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 13:27:46 +0900 Subject: [PATCH 03/16] :+1: Hello, Arguments --- go/study/ch02_first_cli_app/main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 16e2b22a..c6179b09 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -1,5 +1,14 @@ package main +import ( + "fmt" + "os" +) + func main() { - println("Hello, World!") + var name = "someone" + if len(os.Args) > 1 { + name = os.Args[1] + } + fmt.Printf("Hello %s!\n", name) } From 41990271df26dfd09c3f0c8c2dfa3e4e841c983e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 14:02:06 +0900 Subject: [PATCH 04/16] =?UTF-8?q?:+1:=20=E8=B6=B3=E3=81=97=E7=AE=97?= =?UTF-8?q?=E3=82=A2=E3=83=97=E3=83=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch02_first_cli_app/main.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index c6179b09..6feb738b 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -3,12 +3,28 @@ package main import ( "fmt" "os" + "strconv" ) func main() { - var name = "someone" - if len(os.Args) > 1 { - name = os.Args[1] + if len(os.Args) < 3 || 3 < len(os.Args) { + fmt.Println("USAGE:") + fmt.Println("./ch02_first_cli X Y") + fmt.Println(" Shows sum of X and Y") + fmt.Println(" X and Y must be number") + os.Exit(1) + } else if len(os.Args) == 3 { + x, e := strconv.Atoi(os.Args[1]) + if e != nil { + fmt.Printf("ERROR: %q is not a number\n", os.Args[1]) + os.Exit(1) + } + y, e := strconv.Atoi(os.Args[2]) + if e != nil { + fmt.Printf("ERROR: %q is not a number\n", os.Args[2]) + os.Exit(1) + } + fmt.Printf("Rrresult: %d\n", x+y) + os.Exit(0) } - fmt.Printf("Hello %s!\n", name) } From a16534e73b0bc76b1966d5985c748c8d43f5357a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 14:16:37 +0900 Subject: [PATCH 05/16] =?UTF-8?q?:+1:=20=E3=83=98=E3=83=AB=E3=83=97?= =?UTF-8?q?=E3=82=92=E6=8A=9C=E3=81=8D=E5=87=BA=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 足し算と引き算のサポート --- go/study/ch02_first_cli_app/main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 6feb738b..61c9be62 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -28,3 +28,10 @@ func main() { os.Exit(0) } } + +func showHelp() { + fmt.Println("USAGE:") + fmt.Println("./ch02_first_cli (add|subtract) X Y") + fmt.Println(" Shows addition or subtraction with X and Y") + fmt.Println(" X and Y must be number") +} From 432d5f71696b345fd45bbdbeff0f0fe331f8b59a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 14:27:42 +0900 Subject: [PATCH 06/16] =?UTF-8?q?:+1:=20=E8=B6=B3=E3=81=97=E7=AE=97?= =?UTF-8?q?=E3=81=A8=E5=BC=95=E3=81=8D=E7=AE=97=E3=81=AE=E3=82=B5=E3=83=9D?= =?UTF-8?q?=E3=83=BC=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch02_first_cli_app/main.go | 36 ++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 61c9be62..89a8fce3 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -7,24 +7,38 @@ import ( ) func main() { - if len(os.Args) < 3 || 3 < len(os.Args) { - fmt.Println("USAGE:") - fmt.Println("./ch02_first_cli X Y") - fmt.Println(" Shows sum of X and Y") - fmt.Println(" X and Y must be number") + if len(os.Args) < 4 || 4 < len(os.Args) { + showHelp() os.Exit(1) - } else if len(os.Args) == 3 { - x, e := strconv.Atoi(os.Args[1]) - if e != nil { - fmt.Printf("ERROR: %q is not a number\n", os.Args[1]) + } else if len(os.Args) == 4 { + var kind string + switch os.Args[1] { + case "add": + kind = "addition" + case "subtract": + kind = "subtraction" + default: + showHelp() os.Exit(1) } - y, e := strconv.Atoi(os.Args[2]) + x, e := strconv.Atoi(os.Args[2]) if e != nil { fmt.Printf("ERROR: %q is not a number\n", os.Args[2]) os.Exit(1) } - fmt.Printf("Rrresult: %d\n", x+y) + y, e := strconv.Atoi(os.Args[3]) + if e != nil { + fmt.Printf("ERROR: %q is not a number\n", os.Args[3]) + os.Exit(1) + } + var r int + switch kind { + case "addition": + r = x + y + case "subtraction": + r = x - y + } + fmt.Printf("%s: %d\n", kind, r) os.Exit(0) } } From 2ba774d08c4d882f47fc3de2126e7969f290910f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 14:43:12 +0900 Subject: [PATCH 07/16] =?UTF-8?q?:+1:=20strconv..Atoi=20=E3=82=92=E6=8A=9C?= =?UTF-8?q?=E3=81=8D=E5=87=BA=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 足し算と引き算のサポート --- go/study/ch02_first_cli_app/main.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 89a8fce3..7027cd45 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -21,14 +21,14 @@ func main() { showHelp() os.Exit(1) } - x, e := strconv.Atoi(os.Args[2]) + x, e := parseInt(os.Args[2]) if e != nil { - fmt.Printf("ERROR: %q is not a number\n", os.Args[2]) + fmt.Printf("%s\n", e.Error()) os.Exit(1) } - y, e := strconv.Atoi(os.Args[3]) + y, e := parseInt(os.Args[3]) if e != nil { - fmt.Printf("ERROR: %q is not a number\n", os.Args[3]) + fmt.Printf("%s\n", e.Error()) os.Exit(1) } var r int @@ -49,3 +49,11 @@ func showHelp() { fmt.Println(" Shows addition or subtraction 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 +} From 69c335fc4b7d756c8c568f1f7d6f2955b733799d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 15:11:53 +0900 Subject: [PATCH 08/16] =?UTF-8?q?:+:1=20Calculation=20=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=82=BF=E3=83=BC=E3=83=95=E3=82=A7=E3=82=A4=E3=82=B9=E3=82=92?= =?UTF-8?q?=E5=AE=9A=E7=BE=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 条件分岐を減らす --- go/study/ch02_first_cli_app/calc.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 go/study/ch02_first_cli_app/calc.go diff --git a/go/study/ch02_first_cli_app/calc.go b/go/study/ch02_first_cli_app/calc.go new file mode 100644 index 00000000..b62ba484 --- /dev/null +++ b/go/study/ch02_first_cli_app/calc.go @@ -0,0 +1,26 @@ +package main + +type Calculation interface { + Do(x, y int) int + Kind() string +} + +type Addition struct{} + +func (*Addition) Kind() string { + return "addition" +} + +func (*Addition) Do(x, y int) int { + return x + y +} + +type Subtraction struct{} + +func (*Subtraction) Kind() string { + return "subtraction" +} + +func (*Subtraction) Do(x, y int) int { + return x - y +} From 8d98eaf828c31987e30f5991bde5ce174ef00a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 15:17:27 +0900 Subject: [PATCH 09/16] =?UTF-8?q?:+:1=20Calculation=20=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=82=BF=E3=83=BC=E3=83=95=E3=82=A7=E3=82=A4=E3=82=B9=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 条件分岐を減らす --- go/study/ch02_first_cli_app/main.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 7027cd45..80929b9e 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -11,12 +11,12 @@ func main() { showHelp() os.Exit(1) } else if len(os.Args) == 4 { - var kind string + var calc Calculation switch os.Args[1] { case "add": - kind = "addition" + calc = &Addition{} case "subtract": - kind = "subtraction" + calc = &Subtraction{} default: showHelp() os.Exit(1) @@ -31,14 +31,7 @@ func main() { fmt.Printf("%s\n", e.Error()) os.Exit(1) } - var r int - switch kind { - case "addition": - r = x + y - case "subtraction": - r = x - y - } - fmt.Printf("%s: %d\n", kind, r) + fmt.Printf("%s: %d\n", calc.Kind(), calc.Do(x, y)) os.Exit(0) } } From a5b524b6bc2ad1c22aed7db1df97e11a1fd724d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 15:36:17 +0900 Subject: [PATCH 10/16] =?UTF-8?q?:+1:=20map=20=E3=81=A8=20if=20=E3=82=92?= =?UTF-8?q?=E4=BD=BF=E3=81=A3=E3=81=A6=20switch=20=E6=96=87=E3=82=92?= =?UTF-8?q?=E6=B8=9B=E3=82=89=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch02_first_cli_app/main.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 80929b9e..014b62fa 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -6,18 +6,18 @@ import ( "strconv" ) +var calcMap = map[string]Calculation{ + "add": &Addition{}, + "subtract": &Subtraction{}, +} + func main() { if len(os.Args) < 4 || 4 < len(os.Args) { showHelp() os.Exit(1) } else if len(os.Args) == 4 { - var calc Calculation - switch os.Args[1] { - case "add": - calc = &Addition{} - case "subtract": - calc = &Subtraction{} - default: + calc, ok := calcMap[os.Args[1]] + if !ok { showHelp() os.Exit(1) } From 976708264234b2345bf14b527093a1cd8e4d620f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 15:52:11 +0900 Subject: [PATCH 11/16] =?UTF-8?q?:green=5Fheart:=20calc=5Ftest.go=20?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch02_first_cli_app/calc_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 go/study/ch02_first_cli_app/calc_test.go diff --git a/go/study/ch02_first_cli_app/calc_test.go b/go/study/ch02_first_cli_app/calc_test.go new file mode 100644 index 00000000..9bb0a840 --- /dev/null +++ b/go/study/ch02_first_cli_app/calc_test.go @@ -0,0 +1,23 @@ +package main + +import "testing" + +func TestAddtion(t *testing.T) { + calc := &Addition{} + if calc.Kind() != "addition" { + t.FailNow() + } + if calc.Do(1, 2) != 3 { + t.FailNow() + } +} + +func TestSubtraction(t *testing.T) { + calc := &Subtraction{} + if calc.Kind() != "subtraction" { + t.FailNow() + } + if calc.Do(2, 1) != 1 { + t.FailNow() + } +} From 906a43b6537788fb08eac5f0d102d5dd34daa0a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 16:06:59 +0900 Subject: [PATCH 12/16] =?UTF-8?q?:+1:=20multiply=20=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit サブコマンド multiply を実装 --- go/study/ch02_first_cli_app/calc.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/go/study/ch02_first_cli_app/calc.go b/go/study/ch02_first_cli_app/calc.go index b62ba484..54abcead 100644 --- a/go/study/ch02_first_cli_app/calc.go +++ b/go/study/ch02_first_cli_app/calc.go @@ -24,3 +24,13 @@ func (*Subtraction) Kind() string { func (*Subtraction) Do(x, y int) int { return x - y } + +type Multiplication struct{} + +func (*Multiplication) Kind() string { + return "multiplication" +} + +func (*Multiplication) Do(x, y int) int { + return x * y +} From 3790a0c9852c06352432ef002f3d723b8eb4a8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 16:12:04 +0900 Subject: [PATCH 13/16] =?UTF-8?q?:green=5Fheart:=20multiply=20=E3=81=AE?= =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit サブコマンド multiply を実装 --- go/study/ch02_first_cli_app/calc_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/go/study/ch02_first_cli_app/calc_test.go b/go/study/ch02_first_cli_app/calc_test.go index 9bb0a840..02600f8a 100644 --- a/go/study/ch02_first_cli_app/calc_test.go +++ b/go/study/ch02_first_cli_app/calc_test.go @@ -21,3 +21,13 @@ func TestSubtraction(t *testing.T) { t.FailNow() } } + +func TestMultiplication(t *testing.T) { + calc := &Multiplication{} + if calc.Kind() != "multiplication" { + t.FailNow() + } + if calc.Do(2, 3) != 6 { + t.FailNow() + } +} From c2534bf140970bba326d4ca3e904212ac9f598b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Tue, 27 Sep 2022 16:14:17 +0900 Subject: [PATCH 14/16] =?UTF-8?q?:+1:=20mmain=20=E3=81=AB=20multiply=20?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ブコマンド multiply を実装 --- go/study/ch02_first_cli_app/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 014b62fa..002e2cae 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -9,6 +9,7 @@ import ( var calcMap = map[string]Calculation{ "add": &Addition{}, "subtract": &Subtraction{}, + "multiply": &Multiplication{}, } func main() { @@ -38,8 +39,8 @@ func main() { func showHelp() { fmt.Println("USAGE:") - fmt.Println("./ch02_first_cli (add|subtract) X Y") - fmt.Println(" Shows addition or subtraction with X and Y") + fmt.Println("./ch02_first_cli (add|subtract|multiply) X Y") + fmt.Println(" Shows the result of calculation with X and Y") fmt.Println(" X and Y must be number") } From 506f3e72fade513c6bfca960e2e61b5a685f409c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Fri, 7 Oct 2022 14:56:58 +0900 Subject: [PATCH 15/16] =?UTF-8?q?:+1:=20division=20=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch02_first_cli_app/calc.go | 58 +++++++++++++++++++++++++---- go/study/ch02_first_cli_app/main.go | 5 ++- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/go/study/ch02_first_cli_app/calc.go b/go/study/ch02_first_cli_app/calc.go index 54abcead..a0594804 100644 --- a/go/study/ch02_first_cli_app/calc.go +++ b/go/study/ch02_first_cli_app/calc.go @@ -1,18 +1,33 @@ package main +import ( + "fmt" + "strconv" +) + type Calculation interface { - Do(x, y int) int + 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) int { - return x + y +func (*Addition) Do(x, y int) Result { + return Int(x + y) } type Subtraction struct{} @@ -21,8 +36,8 @@ func (*Subtraction) Kind() string { return "subtraction" } -func (*Subtraction) Do(x, y int) int { - return x - y +func (*Subtraction) Do(x, y int) Result { + return Int(x - y) } type Multiplication struct{} @@ -31,6 +46,35 @@ func (*Multiplication) Kind() string { return "multiplication" } -func (*Multiplication) Do(x, y int) int { - return x * y +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} } diff --git a/go/study/ch02_first_cli_app/main.go b/go/study/ch02_first_cli_app/main.go index 002e2cae..d934c9cc 100644 --- a/go/study/ch02_first_cli_app/main.go +++ b/go/study/ch02_first_cli_app/main.go @@ -10,6 +10,7 @@ var calcMap = map[string]Calculation{ "add": &Addition{}, "subtract": &Subtraction{}, "multiply": &Multiplication{}, + "divide": &Division{}, } func main() { @@ -32,14 +33,14 @@ func main() { fmt.Printf("%s\n", e.Error()) os.Exit(1) } - fmt.Printf("%s: %d\n", calc.Kind(), calc.Do(x, y)) + 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) X Y") + 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") } From 866aa4e44823bda38cc7216b2c645fa924d3b7fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EF=BE=86=EF=BD=AC=EF=BD=A7=EF=BD=A7=EF=BD=A7-=E2=95=AC?= =?UTF-8?q?=E2=95=AC=28=E2=95=AC=5E=E0=B2=A0=EF=BD=98=E0=B2=A0=29=E2=99=AD?= Date: Fri, 7 Oct 2022 14:58:12 +0900 Subject: [PATCH 16/16] =?UTF-8?q?:green=5Fheart:=20=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=81=AE=E4=BF=AE=E6=AD=A3=E3=81=A8=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/study/ch02_first_cli_app/calc_test.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/go/study/ch02_first_cli_app/calc_test.go b/go/study/ch02_first_cli_app/calc_test.go index 02600f8a..5d22ad99 100644 --- a/go/study/ch02_first_cli_app/calc_test.go +++ b/go/study/ch02_first_cli_app/calc_test.go @@ -7,7 +7,7 @@ func TestAddtion(t *testing.T) { if calc.Kind() != "addition" { t.FailNow() } - if calc.Do(1, 2) != 3 { + if calc.Do(1, 2) != Int(3) { t.FailNow() } } @@ -17,7 +17,7 @@ func TestSubtraction(t *testing.T) { if calc.Kind() != "subtraction" { t.FailNow() } - if calc.Do(2, 1) != 1 { + if calc.Do(2, 1) != Int(1) { t.FailNow() } } @@ -27,7 +27,22 @@ func TestMultiplication(t *testing.T) { if calc.Kind() != "multiplication" { t.FailNow() } - if calc.Do(2, 3) != 6 { + 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() + } + } +}