An article to help you understand the basic functions of Go language (Part 2)

Release: 2023-07-25 14:19:04
forward
499 people have browsed it

Last Main Review

We know from the above that when defining a function, the function memory allocation can be understood as follows.

An article to help you understand the basic functions of Go language (Part 2)

At the same time, we also know that no matter what operation is performed, it will only operate the stack The above is worth .


Functions and variables

The function name is the variable

I don’t know if you have ever thought about defining a variable and receiving a function, just like this.

package main


import "fmt"


func say() {
    fmt.Println("say")
}


func main() {
    var s1 = say
    s1()
}
Copy after login

The execution results are as follows.

An article to help you understand the basic functions of Go language (Part 2)

It can be found that through a variable receives a Function name, there is no problem when executing it through variable name brackets .

So, what type is this variable? ? ?

fmt.Printf("%T\n",s1)
Copy after login
Copy after login

执行结果

An article to help you understand the basic functions of Go language (Part 2)

如果我将say函数改一下呢?

func say(s int) int{
    fmt.Println("say")
    return 1
}
Copy after login
fmt.Printf("%T\n",s1)
Copy after login
Copy after login

An article to help you understand the basic functions of Go language (Part 2)

可以发现,如果函数参数返回值不一样,打印出来的类型也是不一样的。

定义函数类型

上述我们知道,可以通过变量接收一个函数名

通过变量接收函数名没有约束的,不管函数几个参数,几个返回值,都可以接收,真是活出了动态语言的样子。

定义函数类型就是限制变量接收函数,只能接收指定格式函数

主要用到type关键字。

格式

type 变量名 func([参数类型,参数类型]) [返回值类型]
中括号表示可选参数
Copy after login

例如

type a func()
type b func(int)
type a func(int,int) int
Copy after login

具体代码

package main


import "fmt"


/*
    定义一个函数类型的变量
    接收的函数参数必须是两个int类型
    函数的返回值也必须是int类型
*/
type cType func(int, int) int


func say1(a, b int) int {
    fmt.Println("say",a+b)
    return 1
}
func say2(a, b int) {
    fmt.Println("say")


}
func main() {
    var s1 cType
    s1 = say1//调用s1其实调用的就是say1
    s1(1,1)


    //var s2 cType
    //s2 = say2//报错,cannot use say2 (type func(int, int)) as type cType in assignment
}
Copy after login

高阶函数

千万不要被这个名字唬住了。

简单点说,高阶函数就是把函数当作参数或者把函数当作返回值

函数当作参数

package main


import "fmt"


func add(x int, y int) int {
    return x + y
}
func calc(x int, y int, other func(int, int) int) int {
    return other(x, y)
}
func main() {
    //将add函数传入第三个参数
    var result = calc(34, 12, add)
    fmt.Println(result)
}
Copy after login

函数当作返回值

package main


import "fmt"


func add(x int, y int) int {
  return x + y
}
func test() (func(int, int) int) {
  return add
}
func main() {
  var a = test()
  fmt.Println(a(1,2))
}
Copy after login

至于上述两个的功能,恕小生不才,至今用到的场景不多。

匿名函数

匿名函数顾名思义,就是没有名字的函数。

语法如下

func([参数,参数...])[(返回值,返回值)]{
  代码
}()
//匿名函数后面必须跟括号,直接执行
Copy after login

例如

func()  {
    
}()
func(x int, y int) (int) {
  return x + y
}()
Copy after login

代码

package main


import "fmt"


func main() {
    //s1等于一个匿名函数,并且直接执行
  var s1 = func(x int, y int) (int) {
    return x + y
  }(1,2)
  fmt.Println(s1)
}
Copy after login

闭包

闭包,这个有点不太理解,简单点说就是函数里面套了一个函数里面函数引用的外面函数变量

示例

package main


import "fmt"


func other() func() {
    //返回的是一个函数类型
  var a = 666
  return func() {
        //内部函数使用的是外面函数的a
    fmt.Println(a)
  }
}
func main() {
  var o = other()
  o()
}
Copy after login

执行结果。

An article to help you understand the basic functions of Go language (Part 2)

结果是没有问题的。

虽然我们以前学过,函数执行完毕后,里面的变量会回收。

But when using closures, it can be understood that if the inner function uses the variable of the outer function, then the variable will not be recycled.


##Summary

This article mainly talks about Functions and variables,Higher-order functions,Anonymous functions, Closure, Among them, closure is more difficult to understand, so you must spend some time on it. The edge of a sword comes from sharpening, and the fragrance of plum blossoms comes from the bitter cold. You must practice more.

The above is the detailed content of An article to help you understand the basic functions of Go language (Part 2). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:Go语言进阶学习
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!