- 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.
- UI設(shè)計(jì)基礎(chǔ)培訓(xùn)教程
- 復(fù)雜軟件設(shè)計(jì)之道:領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)全面解析與實(shí)戰(zhàn)
- Learning Linux Binary Analysis
- Python數(shù)據(jù)分析(第2版)
- Java程序設(shè)計(jì)與實(shí)踐教程(第2版)
- C語(yǔ)言實(shí)驗(yàn)指導(dǎo)及習(xí)題解析
- AutoCAD VBA參數(shù)化繪圖程序開(kāi)發(fā)與實(shí)戰(zhàn)編碼
- Mastering Linux Network Administration
- Extreme C
- Simulation for Data Science with R
- Maven for Eclipse
- 精通Spring:Java Web開(kāi)發(fā)與Spring Boot高級(jí)功能
- Swift iOS Programming for Kids
- MonoTouch應(yīng)用開(kāi)發(fā)實(shí)踐指南:使用C#和.NET開(kāi)發(fā)iOS應(yīng)用
- Performance Testing with JMeter 3(Third Edition)