- Go Systems Programming
- Mihalis Tsoukalos
- 206字
- 2021-07-02 18:08:01
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.
- CockroachDB權(quán)威指南
- Learn Type:Driven Development
- Getting Started with ResearchKit
- 劍指JVM:虛擬機(jī)實(shí)踐與性能調(diào)優(yōu)
- Practical Game Design
- 網(wǎng)絡(luò)爬蟲(chóng)原理與實(shí)踐:基于C#語(yǔ)言
- Getting Started with Laravel 4
- Learning Modular Java Programming
- HTML+CSS+JavaScript網(wǎng)頁(yè)設(shè)計(jì)從入門(mén)到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開(kāi)發(fā)視頻大講堂)
- GitHub入門(mén)與實(shí)踐
- Delphi Cookbook
- Learning VMware vCloud Air
- JavaScript Security
- Tableau數(shù)據(jù)可視化從入門(mén)到精通
- Spark內(nèi)核設(shè)計(jì)的藝術(shù):架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)