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

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.

主站蜘蛛池模板: 奉新县| 衡阳县| 霍州市| 德昌县| 仙居县| 廉江市| 磴口县| 漠河县| 绩溪县| 武陟县| 汝阳县| 丽水市| 扬州市| 新河县| 宽城| 永登县| 靖西县| 池州市| 和龙市| 达拉特旗| 岐山县| 房产| 黎平县| 颍上县| 定边县| 灌云县| 千阳县| 通州区| 盘山县| 长海县| 偃师市| 水富县| 科技| 丰原市| 公主岭市| 东明县| 永吉县| 宁化县| 扎赉特旗| 马鞍山市| 荣成市|