- 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.
- Node.js+Webpack開發實戰
- Learning Cython Programming
- Mastering Kotlin
- C語言程序設計立體化案例教程
- TypeScript圖形渲染實戰:基于WebGL的3D架構與實現
- INSTANT Mercurial SCM Essentials How-to
- H5頁面設計:Mugeda版(微課版)
- 零基礎學單片機C語言程序設計
- Keras深度學習實戰
- Building Machine Learning Systems with Python(Second Edition)
- IBM Cognos Business Intelligence 10.1 Dashboarding cookbook
- 零基礎學Java第2版
- 例說FPGA:可直接用于工程項目的第一手經驗
- SQL Server 2014數據庫設計與開發教程(微課版)
- Practical Linux Security Cookbook