Skip to content

Commit 3074397

Browse files
committed
Some Refactoring
1 parent 4b8610b commit 3074397

File tree

3 files changed

+66
-30
lines changed

3 files changed

+66
-30
lines changed

cmd/trygolang/args.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "flag"
4+
5+
// Args は、プログラム引数の値を持つ構造体です1
6+
type Args struct {
7+
OneTime bool
8+
ShowNames bool
9+
}
10+
11+
// NewArgs は、Argsのコンストラクタ関数です
12+
func NewArgs() *Args {
13+
return new(Args)
14+
}
15+
16+
// Parse は、コマンドライン引数を解析しパッケージ変数に格納します
17+
func (a *Args) Parse() {
18+
flag.BoolVar(&a.OneTime, "onetime", false, "run only one time")
19+
flag.BoolVar(&a.ShowNames, "list", false, "show all example names")
20+
21+
flag.Parse()
22+
}

cmd/trygolang/main.go

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"bufio"
5-
"flag"
65
"fmt"
76
"github.com/devlights/try-golang/lib"
87
"log"
@@ -11,9 +10,16 @@ import (
1110
"strings"
1211
)
1312

14-
var mapping = make(lib.SampleMapping)
13+
var (
14+
args *Args
15+
mapping lib.SampleMapping
16+
)
1517

1618
func init() {
19+
args = NewArgs()
20+
args.Parse()
21+
22+
mapping = lib.NewSampleMapping()
1723
mapping.MakeMapping()
1824
}
1925

@@ -34,15 +40,30 @@ func printAllExampleNames() {
3440
}
3541
}
3642

37-
func main() {
38-
var (
39-
onetime = flag.Bool("onetime", false, "run only one time")
40-
showNames = flag.Bool("list", false, "show all example names")
41-
)
43+
func makeCandidates(userInput string) []string {
44+
candidates := make([]string, 0, len(mapping))
45+
for k := range mapping {
46+
if strings.Contains(k, userInput) {
47+
candidates = append(candidates, k)
48+
}
49+
}
50+
51+
return candidates
52+
}
53+
54+
func exec(target string) error {
55+
if v, ok := mapping[target]; ok {
56+
fmt.Printf("[Name] %q\n", target)
57+
if err := v(); err != nil {
58+
return err
59+
}
60+
}
4261

43-
flag.Parse()
62+
return nil
63+
}
4464

45-
if *showNames {
65+
func main() {
66+
if args.ShowNames {
4667
printAllExampleNames()
4768
return
4869
}
@@ -66,36 +87,24 @@ func main() {
6687
break
6788
}
6889

69-
candidates = make([]string, 0, len(mapping))
70-
for k := range mapping {
71-
if strings.Contains(k, userInput) {
72-
candidates = append(candidates, k)
73-
}
74-
}
75-
90+
candidates = makeCandidates(userInput)
7691
numberOfCandidate = len(candidates)
92+
7793
switch {
7894
case numberOfCandidate == 0:
7995
fmt.Printf("Not found...Try Again")
8096
goto nextinput
8197
case numberOfCandidate == 1:
8298
userInput = candidates[0]
83-
if v, ok := mapping[userInput]; ok {
84-
fmt.Printf("[Name] %q\n", userInput)
85-
if err := v(); err != nil {
86-
log.Fatal(err)
87-
}
99+
if err := exec(userInput); err != nil {
100+
log.Fatal(err)
88101
}
89102
case 1 < numberOfCandidate:
90-
// 完全一致するものがあるか?
91103
isPerfectMatchFound := false
92104
for _, c := range candidates {
93105
if c == userInput {
94-
if v, ok := mapping[c]; ok {
95-
fmt.Printf("[Name] %q\n", userInput)
96-
if err := v(); err != nil {
97-
log.Fatal(err)
98-
}
106+
if err := exec(userInput); err != nil {
107+
log.Fatal(err)
99108
}
100109

101110
isPerfectMatchFound = true
@@ -114,7 +123,7 @@ func main() {
114123
}
115124
}
116125

117-
if *onetime {
126+
if args.OneTime {
118127
break
119128
}
120129

lib/mapping.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ import (
3030
"github.com/devlights/try-golang/basic/variables"
3131
)
3232

33-
// サンプル名とサンプル呼び出し関数のマッピング定義の型
33+
// SampleMappingは、サンプル名とサンプル呼び出し関数のマッピング定義を持つ型です
3434
type SampleMapping map[string]func() error
3535

36-
// マッピング生成
36+
// NewSampleMapping は、SampleMappingのコンストラクタ関数です
37+
func NewSampleMapping() SampleMapping {
38+
return make(SampleMapping)
39+
}
40+
41+
// MakeMapping は、マッピング生成します
3742
func (m SampleMapping) MakeMapping() {
3843
m["error01"] = error_.Error01
3944
m["helloworld"] = helloworld.HelloWorld

0 commit comments

Comments
 (0)