官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 靖西县| 金山区| 芦溪县| 乌拉特后旗| 博客| 蓬安县| 平武县| 大庆市| 垦利县| 沙坪坝区| 淄博市| 区。| 阿拉尔市| 射阳县| 洞口县| 读书| 顺昌县| 彭山县| 靖远县| 贵定县| 宁阳县| 吴旗县| 中牟县| 信阳市| 靖西县| 大姚县| 凭祥市| 桂东县| 阿城市| 噶尔县| 临桂县| 祁东县| 政和县| 五峰| 五莲县| 达尔| 商洛市| 开封县| 琼结县| 苍溪县| 阿克苏市|