- Go Systems Programming
- Mihalis Tsoukalos
- 483字
- 2021-07-02 18:08:01
Functions can return error variables
Go functions can return error variables, which means that an error condition can be handled inside a function, outside of a function, or both inside and outside the function; the latter situation does not happen very often. So, this subsection will develop a function that returns error messages. The relevant Go code can be found in funErr.go and will be presented in three parts.
The first part contains the following Go code:
package main import ( "errors" "fmt" "log" ) func division(x, y int) (int, error, error) { if y == 0 { return 0, nil, errors.New("Cannot divide by zero!") } if x%y != 0 { remainder := errors.New("There is a remainder!") return x / y, remainder, nil } else { return x / y, nil, nil } }
Apart from the expected preamble, the preceding code defines a new function named division(), which returns an integer and two error variables. If you remember from your Math classes, when you divide two integer numbers, the division operation is not always perfect, which means that you might get a remainder that is not zero. The errors.New() function from the errors Go package that you see in funErr.go creates a new error variable, using the provided string as the error message.
The second part of funErr.go has the following Go code:
func main() { result, rem, err := division(2, 2) if err != nil { log.Fatal(err) } else { fmt.Println("The result is", result) } if rem != nil { fmt.Println(rem) }
It is a very common Go practice to compare an error variable with nil to quickly find out whether there is an error condition or not.
The last part of funErr.go is as follows:
result, rem, err = division(12, 5) if err != nil { log.Fatal(err) } else { fmt.Println("The result is", result) } if rem != nil { fmt.Println(rem) } result, rem, err = division(2, 0) if err != nil { log.Fatal(err) } else { fmt.Println("The result is", result) } if rem != nil { fmt.Println(rem) } }
This part showcases two erroneous conditions. The first one is an integer division that has a remainder, whereas the second one is an invalid division because you cannot divide a number by zero. As the name log.Fatal() implies, this logging function should be used for critical errors only because when called, it automatically terminates your program. However, as you will see in the next subsection, there exist other, more gentle, ways to log your error messages.
Executing funErr.go generates the next output:
$ go run funErr.go The result is 1 The result is 2 There is a remainder! 2017/03/07 07:39:19 Cannot divide by zero! exit status 1
The last line is automatically generated by the log.Fatal() function, just before terminating the program. It is important to understand that any Go code after the call to log.Fatal() will not be executed.
- ASP.NET Core:Cloud-ready,Enterprise Web Application Development
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- 青少年Python編程入門
- Python機器學習算法: 原理、實現與案例
- Swift 4 Protocol-Oriented Programming(Third Edition)
- C# and .NET Core Test Driven Development
- C指針原理揭秘:基于底層實現機制
- Learning Image Processing with OpenCV
- Learning ECMAScript 6
- Getting Started with Web Components
- Python高性能編程(第2版)
- Build Your Own PaaS with Docker
- Learning QGIS(Second Edition)
- Lucene 4 Cookbook
- MATLAB信號處理與應用