- 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.
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- C#高級編程(第10版) C# 6 & .NET Core 1.0 (.NET開發經典名著)
- Spring 5企業級開發實戰
- 實戰Java程序設計
- VSTO開發入門教程
- 微信小程序開發解析
- Java系統化項目開發教程
- Arduino家居安全系統構建實戰
- 利用Python進行數據分析
- Django 3.0入門與實踐
- Creating Data Stories with Tableau Public
- UI設計全書(全彩)
- Kubernetes進階實戰
- Python商務數據分析(微課版)
- ASP.NET Web API Security Essentials