-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (54 loc) · 1.84 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"flag" // package for command-line flag parsing
"fmt" // package for console output
"os" // package for interacting with the operating system
"strconv" // package for string to integer conversion
"strings" // package for string manipulation
)
func main() {
// define command-line flags
inputFile := flag.String("i", "", "input file")
outputFile := flag.String("o", "", "output file")
flag.Parse()
var decimalNumbers []string
if *inputFile != "" {
// read decimal numbers from input file
fileContent, err := os.ReadFile(*inputFile)
if err != nil {
// print error message to stderr and exit with non-zero status code
fmt.Fprintf(os.Stderr, "error reading file: %v\n", err)
os.Exit(1)
}
// split the file content into whitespace-separated fields (i.e., decimal numbers)
decimalNumbers = strings.Fields(string(fileContent))
} else {
// read decimal numbers from command-line arguments
decimalNumbers = flag.Args()
}
// convert decimal numbers to ASCII text
var asciiText string
for _, decimalStr := range decimalNumbers {
// convert the decimal number string to an integer
decimal, err := strconv.Atoi(decimalStr)
if err != nil {
// print error message to stderr and skip the invalid number
fmt.Fprintf(os.Stderr, "invalid decimal number: %s\n", decimalStr)
continue
}
// convert the decimal number to its ASCII text equivalent and append it to the output string
asciiText += string(decimal)
}
if *outputFile != "" {
// write the output string to the specified output file
err := os.WriteFile(*outputFile, []byte(asciiText), 0644)
if err != nil {
// print error message to stderr and exit with non-zero status code
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
os.Exit(1)
}
} else {
// print the output string to the console
fmt.Println(asciiText)
}
}