Golang understand - function variables

Function variables


As a function of variable values

Function is variable as a function of the value stored in the variable.

In the Golang is a type, function calls, and other types as may be stored in a variable, for example:

package main

// 将函数作为值保存到变量中
import (
        "fmt"
)

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

func main(){
        f := fire()     // 将变量f声明为func()类型,此时f就被俗称为“回调函数”, 此时f的值为nil
        f()
}

Chained calls

链式调用It is a pan-concept, in the end what is the chain of calls is not clear.

  1. Return value of the function is a function, the function call can be chain;

  2. The return value is an object, the method returns the values ​​directly call the object is chained calls.

But the former (1) clearly does not make sense. Truly meaningful chained calls the latter (2), which is the chain method ( Method, Chaining ). Methods chain that is a word, and used very widely. In fact, many of the population 链式调用actually refers to a method chain. But 链式调用the term can also describe the chain of function calls, so it's own existence value becomes difficult to understand.

Advantage of chained calls:

  1. Let calling process closer to natural language.
  2. The original list of arguments turned into a more complex method parameter list simple way to use.
  3. Reduce the amount of code necessary.

Cited above, to

A lot of things in jQuery method is to use the concept of the chain, then in Golang chained calls is kind of how it?

Example : string chaining

Chained calls well represented: the separation of operating and design data

String handling functions (StringProccess) for connection to external data sources: a slicing string (list [] string), also desirable to provide a chaining function slice (chain [] func (string) string)

String chaining design ideas:

  1. This handler can accept a string input, processed output
  2. strings.ToLower () function can be used for each character string passed lowercase func ToLower (s string) string
  3. Internal StringProccess Traversal string string processing functions provided by each data source, each string go through the process again returns after a series of chain sections
package main

import (
    "fmt"
    "strings"
)


//  StringProccess 字符串处理函数,传入字符串切片和处理链
func StringProccess(list []string, chain []func(string) string) {
    // 遍历每一个字符串
    for index, str := range list {
      // 第一个需要处理的字符串
      result := str
      // 遍历每一个处理链
      for _, proc := range chain {
        // 输入一个字符串进行处理,返回数据作为下一个处理链的输入
        result = proc(result)
      }
      // 将结果放回切片
      list[index] = result
    }
}

// 自定义处理函数
// 处理链函数即可以是系统提供的处理函数,也可以使用自定义的函数
// 自定义移除前缀的处理函数
func removePrefix(str string) string {
        return strings.TrimPrefix(str, "go")
}

func main() {
    // 提供待处理的字符串列表
    list := []string{
      "go scanner",
      "go parser",
      "go compiler",
      "go printer",
      "go formater",
    }

    // 处理函数链
    chain := []func(string) string {
      removePrefix,
      strings.TrimSpace,
      strings.ToUpper,
    }

    // 处理字符串
    StringProccess(list, chain)

    // 输出处理好的字符串
    for _, str := range list {
      fmt.Printf(str)
    }
}

to sum up

  1. Chain processor is a common design .Netty programming is to use web application framework written in java language an asynchronous event-driven,
    supports server and client can maintain the rapid development of high-performance-oriented protocol, Netty in respect a similar design chain processor.
  2. Development of the data processing chain thinking and operations split, decoupling, so that developers can according to their technical superiority and demand, system development, and will share their developments to other developers

Guess you like

Origin www.cnblogs.com/vinsent/p/11221486.html