- Go Systems Programming
- Mihalis Tsoukalos
- 218字
- 2021-07-02 18:08:01
The addCLA.go program revisited
This subsection will present an improved version of the addCLA.go program we developed in the previous chapter, to make it able to handle any kind of user input. The new program will be called addCLAImproved.go, but instead of presenting its full Go code, you will only see the differences between addCLAImproved.go and addCLA.go using the diff(1) command-line utility:
$ diff addCLAImproved.go addCLA.go 13,18c13,14 < temp, err := strconv.Atoi(arguments[i]) < if err == nil { < sum = sum + temp < } else { < fmt.Println("Ignoring", arguments[i]) < } --- > temp, _ := strconv.Atoi(arguments[i]) > sum = sum + temp
What this output basically tells us is that the last two lines of code, which can be found in addCLA.go and begin with the > character, were replaced by the lines of code that begin with the < character in addCLAImproved.go. The remaining code of both files is exactly the same.
Executing addCLAImproved.go will generate the following kind of output:
$ go run addCLAImproved.go Sum: 0 $ go run addCLAImproved.go 1 2 -3 Sum: 0 $ go run addCLAImproved.go 1 a 2 b 3.2 @ Ignoring a Ignoring b Ignoring 3.2 Ignoring @ Sum: 3
So, the new and improved version works as expected, behaves reliably, and allows us to differentiate between valid and invalid input.
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- Java性能權威指南(第2版)
- 程序設計基礎教程:C語言
- Active Directory with PowerShell
- 編程改變生活:用Python提升你的能力(進階篇·微課視頻版)
- Visual Basic程序設計(第三版)
- Arduino可穿戴設備開發
- C語言程序設計實踐
- 超好玩的Scratch 3.5少兒編程
- 數據科學中的實用統計學(第2版)
- Python面試通關寶典
- Spring Boot從入門到實戰
- Enterprise Application Architecture with .NET Core
- Java EE應用開發及實訓
- Learning Scrapy