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

Error handling in Go

Errors happen all the time, so it is our job to both catch and handle them, especially when writing code that deals with sensitive system information and files. The good news is that Go has a special data type called error that helps signify erroneous states; if an error variable has a nil value, then there is no error situation.

As you saw in the addCLA.go program that was developed in the previous chapter, you can ignore the error variable that is returned by most Go functions using the _ character:

temp, _ := strconv.Atoi(arguments[i]) 

However, this is not considered good practice and should be avoided, especially on systems software and other kinds of critical software, such as server processes.

As you will see in Chapter 6, File Input and Output, even End of File (EOF) is a type of error that is returned when there is nothing left to read from a file. As EOF is defined in the io package, you can handle it as follows:

if err == io.EOF {

// Do something }

However, the most important task to learn is how to develop functions that return error variables and how to handle them, which is explained next.

主站蜘蛛池模板: 赣州市| 法库县| 宁明县| 呼伦贝尔市| 乡宁县| 剑阁县| 芷江| 敖汉旗| 闵行区| 亳州市| 肥西县| 漳浦县| 兴和县| 余干县| 汝南县| 望城县| 赤水市| 精河县| 苗栗市| 大埔区| 东丽区| 玉门市| 澄迈县| 绩溪县| 平罗县| 湖北省| 包头市| 房山区| 炉霍县| 肥西县| 玛沁县| 云梦县| 吴忠市| 呼玛县| 祥云县| 泰顺县| 棋牌| 高州市| 涞水县| 宁乡县| 楚雄市|