-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
73 lines (64 loc) · 2.17 KB
/
errors.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
// the Go way to handle errors is to have the function/method return an error value as their sole or last return value,
// or nil if no error occured, and for receivers to always check the error they receive.
// functions can also return named variables. These return variables are set to their zero values when the function is entered,
// and keep their zero values unless explicitly assigned to in the body of the function.
func main() {
// multiple assignment is possible b/c the function returns three values
inFilename, outFilename, err := filenamesFromCommandLine()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// files in Go are represented by pointers to values of type os.File. Here we initialize two variables to the standard
// input and output streams
inFile, outFile := os.Stdin, os.Stdout
if inFilename != "" {
if inFile, err = os.Open(inFilename); err != nil {
log.Fatal(err)
}
defer inFile.Close()
}
if outFilename != "" {
if outFile, err = os.Create(outFilename); err != nil {
log.Fatal(err)
}
// leave the file open to work on, but close it as soon as the enclosing function, in this case main(), returns, thereby ensuring
// that the file is closed when the program is done with it
defer outFile.Close()
}
if err = americanise(inFile, outFile); err != nil {
log.Fatal(err)
}
}
func filenamesFromCommandLine() (inFilename, outFilename string, err error) {
if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
err = fmt.Errorf("usage: %s [<]infile.txt [>]outfile.txt",
filepath.Base(os.Args[0]))
return "", "", err
}
if len(os.Args) > 1 {
inFilename = os.Args[1]
if len(os.Args) > 2 {
outFilename = os.Args[2]
}
}
if inFilename != "" && inFilename == outFilename {
log.Fatal("won't overwrite the infile")
}
// if all goes well, return two strings and an error value of nil
return inFilename, outFilename, nil
}
// for a value to be readable it must satisfy the io.Reader interface, and for it to be writeable it must satisfy the io.Writer interface.
// the bufio package provides buffered input/output