Home >Backend Development >Golang >Share quality notes: Learn how to use go closures

Share quality notes: Learn how to use go closures

藏色散人
藏色散人forward
2021-08-26 16:36:412213browse

The following tutorial column of go language will introduce you to the learning and use of go closures. I hope it will be helpful to friends in need!

go closure

In Golang, a closure is a Functions on variables outside the scope.
In other words, a closure is an inner function that has access to variables within the scope in which it was created. Even if the external function completes execution and the scope is destroyed, it can still be accessed.
Before delving into closures, you need to understand what anonymous functions are.

Anonymous function

As the name suggests, an anonymous function is a function without a name.
For example, we create a general function and an anonymous function

package mainimport (
    "fmt")func sayhi1() { // 一般函数
    fmt.Println("hello golang, I am Regular function")}func main() {
    sayhi1()
    sayhi2 := func() { //匿名函数
        fmt.Println("hello golang, I am Anonymous function")
    }
    sayhi2()}

Result output:

hello golang, I am Regular functionhello golang, I am Anonymous function

Use anonymous functions by creating a function that returns a function.

package mainimport (
    "fmt")func sayhello(s string) func() {
    return func() {
        fmt.Println("hello", s)
    }}func main() {
    sayhello := sayhello("golang")
    sayhello()}

Result output:

hello golang

The only difference between regular functions and anonymous functions is that anonymous functions are not declared at the package level, they are declared dynamically and are usually either used, forgotten, or Is assigned to a variable for later use.

The essence of closure

A closure is a block of code that contains free variables that are not within this code block or any global context defined, but in the environment in which the code block is defined. Since the free variables are contained in the code block, these free variables and the objects they refer to will not be released as long as the closure is still used. The code to be executed provides a bound computing environment for the free variables.
The value of closures is that they can be used as function objects or anonymous functions. For the type system, this means not only representing data but also code. Most languages ​​that support closures use functions as first-level objects, which means that these functions can be stored in variables and passed as parameters to other functions. The most important thing is that they can be dynamically created and returned by functions.

Closures in Golang will also reference variables outside the function. The implementation of closures ensures that as long as the closure is still used, the variables referenced by the closure will always exist. Formally, anonymous functions are closures.
Let’s look at a closure example:

package mainimport (
    "fmt")func caller() func() int {
    callerd := 1
    sum := 0
    return func() int {
        sum += callerd        return sum    }}func main() {
    next := caller()
    fmt.Println(next())
    fmt.Println(next())
    fmt.Println(next())}

Result output:

1
2
3

In this example, called and sum are free variables, and the anonymous function returned by the caller function provides calculations for the free variables. The code block composed of the environment, anonymous functions and free variables is actually a closure. In the closure function, only anonymous functions can access the free variables called and sum, but cannot access them through other means, so the closure free variables are safe.
According to the rules of the imperative language, the caller function only returns the address of the anonymous function, but when the anonymous function is executed, an error will occur because the sum and called variables cannot be found in its scope. In functional languages, when an embedded function body references a variable outside the body, the reference environment and function body involved in the definition will be packaged into a whole (closure) and returned. The use of closures is no different from normal function calls.

Now we give the definition of a reference environment: a set of all active constraints at a certain point in program execution, where a constraint refers to the name of a variable and the object it represents. the relationship between.
So we say: closure = function reference environment

In fact, we can regard the closure function as a class (C). A closure function call is to instantiate an object and the free variable of the closure They are the member variables of the class, and the parameters of the closure function are the parameters of the function object of the class. In this example, next can be regarded as an instantiated object, and next() can be regarded as the return value of the object function call.
This reminds us of a famous saying: Objects are data with behavior, and closures are behaviors with data

Some examples of closure usage

  • Using closures to achieve data isolation
    Suppose you want to create a function that can access data that persists even after the function exits. For example, if you want to count the number of times a function has been called, but don't want anyone else to have access to that data (so they don't accidentally change it), you can do it with a closure:
package mainimport (
    "fmt")func caller() func() int {
    callerd := 0
    return func() int {
        callerd++
        return callerd    }}func main() {
    next := caller()
    fmt.Println(next())
    fmt.Println(next())
    fmt.Println(next())}

Result output:

1
2
3
  • 利用闭包包装函数和创建中间件
    Go 中的函数是一等公民。这意味着您不仅可以动态创建匿名函数,还可以将函数作为参数传递给函数。例如,在创建 Web 服务器时,通常会提供一个功能来处理Web 请求到特定的路由。
package mainimport (
  "fmt"
  "net/http")func main() {
  http.HandleFunc("/hello", hello)
  http.ListenAndServe(":3000", nil)}func hello(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintln(w, "<h1>Hello!</h1>")}

在上面例子中,函数 hello() 被传递给 http.HandleFunc() 函数,并在该路由匹配时调用。
虽然这段代码不需要闭包,但如果我们想用更多逻辑包装我们的处理程序,闭包是非常有用的。一个完美的例子是我们可以通过创建中间件来在我们处理程序执行之前或之后做一些其它的工作。
什么是中间件?
中间件基本上是可重用功能的一个奇特术语,它可以在设计用于处理 Web 请求的代码之前和之后运行代码。在 Go 中,这些通常是通过闭包来实现的,但在不同的编程语言中,可以通过其他方式来实现。
在编写 Web 应用程序时使用中间件很常见,而且它们不仅可用于计时器(您将在下面看到一个示例)。例如,中间件可用于编写代码验证用户是否登录过一次,然后将其应用到你的所有会员专页。
让我们看看一个简单的计时器中间件在 Go 中是如何工作的。

package mainimport (
  "fmt"
  "net/http"
  "time")func main() {
  http.HandleFunc("/hello", timed(hello))
  http.ListenAndServe(":3000", nil)}func timed(f func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) {
  return func(w http.ResponseWriter, r *http.Request) {
    start := time.Now()
    f(w, r)
    end := time.Now()
    fmt.Println("The request took", end.Sub(start))
  }}func hello(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintln(w, "<h1>Hello!</h1>")}

timed() 函数接受一个可以用作处理函数的函数,并返回一个相同类型的函数,但返回的函数与传递它的函数不同。返回的闭包记录当前时间,调用原始函数,最后记录结束时间并打印出请求的持续时间。同时对我们的处理程序函数内部实际发生的事情是不可知的。
现在我们要做的就是将我们的处理程序包装在 timed(handler) 中并将闭包传递给 http.HandleFunc() 函数调用。

  • 利用闭包使用 sort 二分搜索
    使用标准库中的包也经常需要闭包,例如 sort 包。这个包为我们提供了大量有用的功能和代码,用于排序和搜索排序列表。例如,如果您想对一个整数切片进行排序,然后在切片中搜索数字 7,您可以像这样使用 sort 包。
package mainimport (
  "fmt"
  "sort")func main() {
  numbers := []int{1, 11, -5, 7, 2, 0, 12}
  sort.Ints(numbers)
  fmt.Println("Sorted:", numbers)
  index := sort.SearchInts(numbers, 7)
  fmt.Println("7 is at index:", index)}

结果输出:

Sorted: [-5 0 1 2 7 11 12]7 is at index: 4

如果要搜索的每个元素都是自定义类型的切片会发生什么?或者,如果您想找到第一个等于或大于 7 的数字的索引,而不仅仅是 7 的第一个索引?
为此,您可以使用 sort.Search() 函数,并且您需要传入一个闭包,该闭包可用于确定特定索引处的数字是否符合您的条件。
sort.Search() is a binary search
sort.Search 函数执行二分搜索,因此它需要一个闭包,该闭包在满足您的条件之前对任何索引返回 false,在满足后返回 true。
让我们使用上面描述的示例来看看它的实际效果;我们将搜索列表中第一个大于或等于 7 的数字的索引。

package mainimport (
    "fmt"
    "sort")func main() {
    numbers := []int{1, 11, -5, 8, 2, 0, 12}
    sort.Ints(numbers)
    fmt.Println("Sorted:", numbers)

    index := sort.Search(len(numbers), func(i int) bool {
        return numbers[i] >= 7
    })
    fmt.Println("The first number >= 7 is at index:", index)
    fmt.Println("The first number >= 7 is:", numbers[index])}

结果输出:

Sorted: [-5 0 1 2 8 11 12]The first number >= 7 is at index: 4
The first number >= 7 is: 8

在这个例子中,我们的闭包是作为第二个参数传递给 sort.Search() 的简单函数。
这个闭包访问数字切片,即使它从未被传入,并为任何大于或等于 7 的数字返回 true。通过这样做,它允许 sort.Search() 工作而无需了解什么您使用的基础数据类型是什么,或者您试图满足什么条件。它只需要知道特定索引处的值是否符合您的标准。

  • 用闭包+defer进行处理异常
package mainimport (
    "fmt")func handle() {
    defer func() {
        err := recover()
        if err != nil {
            fmt.Println("some except had happend:", err)
        }
    }()
    var a *int = nil
    *a = 100}func main() {
    handle()}

结果输出:

some except had happend: runtime error: invalid memory address or nil pointer dereference

recover函数用于终止错误处理流程。一般情况下,recover应该在一个使用defer关键字的函数中执行以有效截取错误处理流程。如果没有在发生异常的goroutine中明确调用恢复过程(调用recover函数),会导致该goroutine所属的进程打印异常信息后直接退出
对于第三方库的调用,在不清楚是否有panic的情况下,最好在适配层统一加上recover过程,否则会导致当前进程的异常退出,而这并不是我们所期望的。

                                      

The above is the detailed content of Share quality notes: Learn how to use go closures. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete