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

Illustrating Go functions

This subsection will present examples of the previous types of functions using the Go code of the functions.go program. The first part of the program contains the expected preamble and the implementation of the unnamedMinMax() function:

package main 
 
import ( 
   "fmt" 
) 
 
func unnamedMinMax(x, y int) (int, int) { 
   if x > y { 
         min := y 
         max := x 
         return min, max 
   } else { 
         min := x 
         max := y 
         return min, max 
   } 
} 

The unnamedMinMax() function is a regular function that gets two integer numbers as input, named x and y, respectively. It returns two integer numbers as output using a return statement.

The next part of functions.go defines another function but this time with named returned values, which are called min and max:

func minMax(x, y int) (min, max int) { 
   if x > y { 
         min = y 
         max = x 
   } else { 
         min = x 
         max = y 
   } 
   return min, max 
} 

The next function is an improved version of minMax() because you do not have to explicitly define the return variables of the return statement:

func namedMinMax(x, y int) (min, max int) { 
   if x > y { 
         min = y 
         max = x 
   } else { 
         min = x 
         max = y 
   } 
   return 
} 

However, you can easily discover which values will be returned by looking at the definition of the namedMinMax() function. The namedMinMax() function will return the current values of min and max, in that order.

The next function shows how to sort two integers without having to use a temporary variable:

func sort(x, y int) (int, int) { 
   if x > y { 
         return x, y 
   } else { 
         return y, x 
   } 
} 

The previous code also shows how handy it is that Go functions can return more than one value. The last part of functions.go contains the main() function; this could be explained in two parts.

The first part is to do with anonymous functions:

 func main() {
   y := 4 
   square := func(s int) int { 
         return s * s 
   } 
   fmt.Println("The square of", y, "is", square(y)) 
 
   square = func(s int) int { 
         return s + s 
   } 
   fmt.Println("The square of", y, "is", square(y)) 

Here, you define two anonymous functions: the first one calculates the square of the given integer whereas the second doubles the given integer number. What is important here is that both of them are assigned to the same variable, which is a totally wrong and is a dangerous practice. Therefore, improper use of anonymous functions can create nasty bugs, so take extra care and do not assign the same variable to different anonymous functions.

Note that even if a function is assigned to a variable, it is still considered an anonymous function.

The second part of main() uses some of the defined functions:

   fmt.Println(minMax(15, 6)) 
   fmt.Println(namedMinMax(15, 6)) 
   min, max := namedMinMax(12, -1) 
   fmt.Println(min, max) 
} 

What is interesting here is that you can get the two returned values of the namedMinMax() function using two variables, all in one statement.

Executing functions.go generates the following output:

$ go run functions.go
The square of 4 is 16
The square of 4 is 8
6 15
6 15
-1 12

The next section shows more examples of anonymous functions combined with the defer keyword.

主站蜘蛛池模板: 铜川市| 钦州市| 澎湖县| 阆中市| 龙井市| 青州市| 成都市| 甘泉县| 甘德县| 石屏县| 尖扎县| 张掖市| 佛学| 灵丘县| 平陆县| 通化市| 镇赉县| 莫力| 罗江县| 崇文区| 屯门区| 军事| 德昌县| 陆川县| 武邑县| 伊吾县| 托克托县| 托克托县| 竹溪县| 天镇县| 同江市| 澄城县| 克山县| 连江县| 大关县| 锡林郭勒盟| 尼玛县| 高碑店市| 德兴市| 盐津县| 墨玉县|