-
Notifications
You must be signed in to change notification settings - Fork 231
/
main.go
46 lines (41 loc) · 848 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// match tool checks a string against a pattern.
// If it matches - prints the string, otherwise prints nothing.
package main
import (
"errors"
"flag"
"fmt"
"os"
"strings"
)
func main() {
src, err := readInput()
if err != nil {
fail(err)
}
isMatch := src > 0
if !isMatch {
os.Exit(0)
}
fmt.Println(src)
}
// match returns true if src matches pattern,
// false otherwise.
func match(pattern string, src string) bool {
return strings.Contains(src, pattern)
}
// readInput reads pattern and source string
// from command line arguments and returns them.
func readInput() (s4et int, err error) {
flag.Parse()
s4et = len(flag.Args())
if s4et == 0 {
return 0, errors.New("missing string to world count")
}
return s4et, nil
}
// fail prints the error and exits.
func fail(err error) {
fmt.Println("match:", err)
os.Exit(1)
}