Go Functional Programming

 

 

Function declaration

In the Go language, common function declaration syntax is as follows:

functionName FUNC (parameterName type) returnType {   
    // function body (specific functions implemented) 
}

Function declaration keyword to  func start, followed by the name of a custom function  functionname (函数名). In the parameter list defined functions  ( and  ) between, the return type of the following defines  returntype (返回值类型)at. The syntax for declaring a parameter using  the parameter name  parameter type  of way, any number of similar parameters  (parameter1 type, parameter2 type) 即(参数1 参数1的类型,参数2 参数2的类型)specified in the form. After contained in  { and  } code between, it is the function thereof.

The list of parameters and return values ​​of the function is not required, so the following function declarations are also effective

functionName FUNC () {   
    // Annotation: This function does not need to represent the input parameters and returns no value 
}

  

The sample program

main Package 

Import "FMT" 


// single parameter passed 
FUNC functionname1 (parameterName int) { 
	fmt.Printf ( "Parameters:% D \ n-", parameterName); 
} 

// pass a single parameter, a single return value 
func functionname2 (parametername int) {int 
	fmt.Printf ( "parameters:% D \ n-", parameterName); 
	return parameterName * parameterName; 
} 

// pass the plurality of parameters 
FUNC functionname3 (parametername1 int, int parametername2) { 
	fmt.Printf ( "parameter 1 :% D \ n-", parametername1); 
	fmt.Printf (" parameter 2:% D \ n-", parametername2); 
} 

// pass a plurality of parameters, a plurality of return value 
func functionname4 (parametername1 int, parametername2 int ) ( int, int) { 
	fmt.Printf ( "parameters. 1: D% \ n-", parametername1);  
	fmt.Printf ( "parameter 2:% d \ n", parametername2);
	return parametername1 * parametername2,parametername1 + parametername2;
}



func main(){
	a := 1;
	b := 2;
	functionname1(a);
	functionname2(a);
	functionname3(a,b);
	functionname4(a,b);
}

  

Named return value

It may return a value from the function name. Once named return values, these values ​​can be considered the first line of the function is declared as variables.

RectProps above function may also be written in this way:

rectProps FUNC (length, width float64) (Area, perimeter float64) {   
    Area = length * width 
    perimeter = (length + width) * 2 
    return // do not need to explicitly specify the return value returned by default Area, the value of the perimeter 
}

Please note that the return statement in the function does not explicitly return a value. Since the  area  and  perimeter  specified in the function declaration to return a value, so when faced with a return statement, they will automatically return from the function.

 

Whitespace

_  Is used as a blank character in Go, any value can be used to represent any type.

We continue to  rectProps function as an example, the function calculates the area and perimeter of is. If we only need to calculate the area, perimeter and do not care about the results, how to call this function? At this time, whitespace  _  to turn.

The following program we only used the function  rectProps a return value area

package main

import (  
    "fmt"
)

func rectProps(length, width float64) (float64, float64) {  
    var area = length * width
    var perimeter = (length + width) * 2
    return area, perimeter
}

func main() {  
    area, _ := rectProps(10.8, 5.6) // 返回值周长被丢弃
    fmt.Printf("Area %f ", area)
}

Run the program

In the program of  area, _ := rectProps(10.8, 5.6) this line, we see white space  _ used to skip unwanted results.


refer:
"GCTT produced" Go series of tutorials - 6. function (Function)


Guess you like

Origin www.cnblogs.com/-wenli/p/11780013.html