当前位置:网站首页>[golang] quick review guide quickreview (IV) -- functions

[golang] quick review guide quickreview (IV) -- functions

2022-06-23 20:22:00 DDGarfield

function

C# The function in

1. Variable parameters params

static int IntSum(int x, int y)
{
    return x + y;
}

// heavy load , Variable parameters 
static int IntSum(params int[] x)
{
    int sum = 0;
    for (int i = 0; i < x.Length; i++)
    {
        sum += x[i];
    }
    return sum;
}

2. Return multiple values

2.1 Parameters out

static void Calc(int x, int y, out int sum, out int sub)
{
    sum = x + y;
    sub = x - y;
}

2.2 Tuple- Tuples

// Tuples 
// static (int, int) Calc(int x, int y)
static (int sum, int sub) Calc(int x, int y)
{
    return (x + y, x - y);
}

3.delegate entrust ( Function type )

C# The function type in is a delegate

public delegate (int, int) CalculationHandler(int x, int y);

// Use 
static void TestDelegate(CalculationHandler handler)
{

}

// Anonymous functions   Yes delegate keyword , No function signature 
TestDelegate(delegate (int x, int y)
             {
                 return (x + y, x - y);
             });

// Call to transfer value ,lambda expression , hold delegate Omit directly 
TestDelegate((x, y) =>
             {
                 return (x + y, x - y);
             });

Golang The function in

Golang The return value of is after the function name and parameters , Functions must be annotated , And start with the function name , otherwise golint There will be a green prompt

1. Variable parameters ...

// IntSum  Write notes 
func IntSum(x int, y int) int {
    return x + y
}

// go Function overloading is not supported , Therefore, the function signatures in the same package are not consistent 
func Sum(x ...int) int {
 // here x Is an array 
 sum := 0
 for _, v := range x {
  sum = sum + v
 }
 return sum
}

2. Return multiple values

See more forms , If for no other , Just to understand the source code of others

func Calc(x int, y int) (sum int,sub int) {
 sum = x + y
 sub = x - y
 return sum, sub
}
func Calc(x int, y int) ( int, int) {
    sum := x + y
    sub := x - y
 return sum, sub
}
func Calc(x, y int) (int, int) {
 sum := x + y
 sub := x - y
 return sum, sub
}

3. Function type

Function as a type ,C# Created the Commission , Looks more elegant , and Golang The definition of is more direct :

type calculation func(int, int) (int, int)

3.1 Use

func main() {
    var calc calculation
    calc = addOrSub
    sum, sub := calc(5, 6)
 fmt.Printf("result:sum-%v sub-%v \n", sum, sub)
    sum1, sub2 := testFuncType(8, 6, addOrSub)
    fmt.Printf("result:sum-%v sub-%v \n", sum1, sub2)
}
type calculation func(int, int) (int, int)

func addOrSub(x, y int) (int, int) {
 return x + y, x - y
}

// testFuncType  This is more like a commission Action Func<> Use 
func testFuncType(x, y int, op calculation) (int, int) {
 return op(x, y)
}

3.2 Anonymous functions

Golang Anonymous functions of are not like C# like that , need delegate, It is without any keywords , The use is also simple and rough :

addOrSubTest := func(x, y int) (int, int) {
    return x + y, x - y
}
sum3, sub3 := addOrSubTest(9, 6)
fmt.Printf("anonymous function:sum-%v sub-%v \n", sum3, sub3)

4. The return value is the function

Since the function is a type , It can also be used as the return value of the function :

func do(s string) func(int, int) int {
 switch s {
 case "+":
  return func(x int, y int) int {
   return x + y
  }
 case "-":
  return func(x int, y int) int {
   return x - y
  }
 default:
  return nil
 }
}

5. New content defer

defer Statement will delay the statement that follows it . The function is about to return, Press the deferred statement to defer The definition is executed in reverse order , in other words , First define defer The statement of is finally executed , Finally defined defer The sentence of , The first to perform . Like a stack , First in, then out , Last in, first out .

func main() {
    var calc calculation
    calc = addOrSub
    sum, sub := calc(5, 6)
 fmt.Printf("result:sum-%v sub-%v \n", sum, sub)
}
type calculation func(int, int) (int, int)
func addOrSub(x, y int) (int, int) {
 defer fmt.Println("addOrSub1")
 defer fmt.Println("addOrSub2")
 defer fmt.Println("addOrSub3")
 fmt.Printf("result:%v and %v\n", x+y, x-y)
 return x + y, x - y
}
result:11 and -1
addOrSub3
addOrSub2
addOrSub1
result:sum-11 sub--1

More complicated ( Cheating father ) Of defer Example , Please refer to some interview questions .

Again : This series is not a tutorial , If you want to learn systematically , Bloggers can recommend learning resources .

原网站

版权声明
本文为[DDGarfield]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231850564400.html