官术网_书友最值得收藏!

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.

Note that most Go functions return an error variable, which should always be examined, especially on production software.

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.

Go has support for four different sizes of signed and unsigned integers, named int8, int16, int32, int64, uint8, uint16, uint32, and uint64, respectively. However, Go also has int and uint, which are the most efficient signed and unsigned integers for your current platform. Therefore, when in doubt, use either int or uint.

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 this is not a perfect situation, it is not that bad if you know that your program does not work as expected for some given kinds of input. The bad thing is when the developer has no idea that there exist certain kinds of input that can make a program fail, because you cannot correct what you do not believe or recognize is wrong.

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.

主站蜘蛛池模板: 洱源县| 长兴县| 唐河县| 胶州市| 安丘市| 那坡县| 江油市| 共和县| 克什克腾旗| 永仁县| 尉氏县| 九龙坡区| 彭阳县| 乌什县| 敦煌市| 通化市| 乐昌市| 科技| 沅江市| 罗甸县| 贺州市| 格尔木市| 佛坪县| 调兵山市| 新安县| 吉隆县| 绍兴市| 盘锦市| 瑞金市| 南宁市| 镶黄旗| 左云县| 宁津县| 宁河县| 萝北县| 盐亭县| 宜黄县| 秭归县| 达尔| 嵊州市| 临海市|