- Go Systems Programming
- Mihalis Tsoukalos
- 793字
- 2021-07-02 18:07:57
Finding the sum of the command-line arguments
Now, let us try something different and tricky: you are going to try to find the summary of the command-line arguments given to your Go program. Therefore, you are going to consider the command-line arguments as numbers. Although the main idea remains the same, the implementation is totally different because you will have to convert your command-line arguments into numbers. The name of the Go program will be addCLA.go, and it can be split into two parts.
The first part is the preamble of the program:
package main import ( "fmt" "os" "strconv" )
You need the fmt package for printing your output and the os package for reading the command-line arguments. As command-line arguments are stored as strings, you will also need the srtconv package for converting them into integers.
The second part is the implementation of the main() function:
func main() { arguments := os.Args sum := 0 for i := 1; i < len(arguments); i++ { temp, _ := strconv.Atoi(arguments[i]) sum = sum + temp } fmt.Println("Sum:", sum) }
The strconv.Atoi() function returns two values: the first one is an integer number, provided that the conversion was successful, and the second one is an error variable.
If you do not use the strconv.Atoi() function, then you will have two problems:
- The first one is that the program will try to perform additions, which are mathematical operations, using strings
- The second one is that you will not be able to tell whether a command-line argument is a valid integer number or not, which can be done by examining the return value of strconv.Atoi()
So, strconv.Atoi() not only does the desired job, but it also tells us whether a given argument is a valid integer or not, which is equally important because it allows us to process inappropriate arguments differently.
The other crucial Go code found in addCLA.go is the one that ignores the value of the error variable from the strconv.Atoi() function using pattern matching. The _ character means "match everything" in Go pattern matching terms, but do not save it in any variable.
Executing addCLA.go with the right kind of command-line arguments creates the following output:
$ go run addCLA.go 1 2 -1 -3 Sum: -1 $ go run addCLA.go Sum: 0
The good thing is that addCLA.go does not crash if it gets no arguments, without you taking care of it. Nevertheless, it would be more interesting to see how the program handles erroneous input because you can never assume that you are going to get the right type of input:
$ go run addCLA.go ! Sum: 0 $ go run addCLA.go ! -@ Sum: 0 $ go run addCLA.go ! -@ 1 2 Sum: 3
As you can see, if the program gets the wrong type of input, it does not crash and does not include the erroneous input in its calculations. What is a major issue here is that addCLA.go does not print any warning message to let the user know that some of their input was ignored. This kind of dangerous code creates unstable executables that might generate security issues when given the wrong kind of input. So, the general advice here is that you should never expect or rely on the Go compiler, or any other compiler or program, to take care of such things because this is your job.
Chapter 3, Advanced Go Features, will talk about error handling in Go in more detail and will present a better and safer version of the previous program. For now, we should all be happy that we can prove that our program does not crash with any kind of input.
Although processing command-line arguments looks easy, it might get pretty complex if your command-line utility supports a large number of options and parameters. Chapter 5, Files and Directories, will talk more about processing command-line options, arguments, and parameters using the flag standard Go package.
- Python自動(dòng)化運(yùn)維快速入門
- Vue.js入門與商城開發(fā)實(shí)戰(zhàn)
- 云原生Spring實(shí)戰(zhàn)
- Python Data Analysis Cookbook
- 微服務(wù)架構(gòu)深度解析:原理、實(shí)踐與進(jìn)階
- Domain-Driven Design in PHP
- IoT Projects with Bluetooth Low Energy
- 奔跑吧 Linux內(nèi)核
- Java程序設(shè)計(jì)教程
- Python面試通關(guān)寶典
- Java從入門到精通(視頻實(shí)戰(zhàn)版)
- 3D Printing Designs:Octopus Pencil Holder
- 軟件測(cè)試(慕課版)
- Mastering Object:Oriented Python(Second Edition)
- RESTful Web API Design with Node.js(Second Edition)