- 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.
- Extending Jenkins
- Spring 5企業級開發實戰
- Visual FoxPro 程序設計
- FreeSWITCH 1.6 Cookbook
- Animate CC二維動畫設計與制作(微課版)
- Java設計模式及實踐
- Python面向對象編程:構建游戲和GUI
- 名師講壇:Spring實戰開發(Redis+SpringDataJPA+SpringMVC+SpringSecurity)
- Android開發案例教程與項目實戰(在線實驗+在線自測)
- SQL Server與JSP動態網站開發
- MATLAB GUI純代碼編寫從入門到實戰
- Python語言科研繪圖與學術圖表繪制從入門到精通
- Applied Deep Learning with Python
- 網頁設計與制作
- Python滲透測試編程技術:方法與實踐(第2版)