Golang study notes-function as value use

Source: Internet
Author: User

Concept

在C和C++中,函数是一组指令的集合,这组指令的入口地址用函数名来表示,所以,在C和C++中,函数名称是一个地址。  

In the go language, however, a function is a type and is the first type (first-class). His status is the same as the type int string.
We often declare a variable of value type int or string, and now we can declare a variable with a value type of a function called a function variable that is, the function can be assigned as a value type to the variable

Example:

func Add(a,b int)int{    return a+b}func simple01(){    //声明一个int类型的变量    var number = 3    fmt.Println(number)    //声明一个string类型的变量    str:="aabbcc"    fmt.Println(str)    //现在我们声明一个函数类型的变量    myFunc:=Add    //通过函数变量调用函数(用函数指针来理解,还是很容易的)    fmt.Println(myFunc(1,2))}

Execution Result:

3
Aabbcc
3

Use of function variables

In the
most basic usage, a function variable is passed directly as a value, such as: an argument passed into a function can pass in a function variable
Another way to use, the most common method, is to use the TYEP keyword to define a custom class type with the underlying type as the function type. When a function is assigned to such a variable as a value, the signature of the function must be the same (parameter and return value of the function)

Example:

type ProcessBasename func(string)string//basename函数 移除字符串的路径部分和.后缀 只取文件名func basename(str string)string{    //去除路径部分    slash:=strings.LastIndex(str,"/") //如果没有找到"/" LastIndex返回-1    str=str[slash+1:]    //取出.的前一部分    if dot:=strings.LastIndex(str,".");dot>0{        str = str[:dot]    }    return str}//这个函数使用了函数变量作为参数func GetBaseName(str string,myBasenameFunc func(string)string)string{    return myBasenameFunc(str)}//这个函数使用了type关键字自定义的类型(底层是一种函数类型)func GetBaseName1(str string,processBasename ProcessBasename)string{    return processBasename(str)}//函数变量被当做函数参数传递func simple02(){    str:="a/b/c.go"    //将函数basename作为实参传递给GetBaseName    //要注意,函数变量basename的函数签名要和形参myBasenameFunc的函数签名完全一致    //在这里,函数签名是指 函数的参数和返回值    fmt.Println(GetBaseName(str,basename))    str1:="c.d.go"    fmt.Println(GetBaseName1(str1,basename))}

Execution Result:

C
C.D

Summarize

the steps and meanings of using function variables
    1.定义一个底层为函数类型的自定义类型      2.实现定义的函数类型      3.作为参数调用  
using a function variable allows the program to execute logic based on the actual function passed by the user, where the function type is called. Makes the program more flexible

Example: In the program according to the actual function passed by the user to achieve two numbers plus or minus

type CalculateFunc func(a,b int)intfunc Add(a,b int)int{    return a+b}func Sub(a,b int)int{    return a-b}func Calculate(a,b int,calculateFunc CalculateFunc)int{    return calculateFunc(a,b)}func simple03(){    a,b:=5,3    //相加    add:=Calculate(a,b,Add)    //相减    sub:=Calculate(a,b,Sub)    println(add,sub)}func main(){    simple01()    simple02()    simple03()}

Execution Result:

8 2
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.