Home > Backend Development > Golang > The essence and usage of golang function pointers

The essence and usage of golang function pointers

王林
Release: 2024-04-26 09:18:01
Original
904 people have browsed it

In the Go language, the function pointer points to the entry address of the function and can be stored in a variable or passed to the function. Using function pointers helps decouple the code that calls a function from the code that implements it. It can be used to store functions, pass functions, or return functions. One application scenario is to create sortable key-value pairs, define custom sorting rules through function pointers, and sort the key-value pairs in ascending order by value.

The essence and usage of golang function pointers

Function pointer in Go language

Function pointer is a pointer to a function. In Go, functions are first-class values, so they can be stored in variables, passed to functions, or returned from functions. The main advantage of using function pointers is that it decouples the code that calls the function from the code that implements the function.

The essence of function pointer

The function pointer is essentially a pointer pointing to the function entry address. In the Go language, the type of function pointer is func(*args)(*result), where:

  • *args is the function parameter type Pointer
  • *result is the value or pointer of the function return type

Usage of function pointer

1. Store function

func add(a, b int) int {
  return a + b
}

func main() {
  // 将 add 函数存储在变量 f 中
  f := add

  // 通过 f 调用 add 函数
  fmt.Println(f(1, 2)) // 输出:3
}
Copy after login

2. Pass to function

func apply(f func(int) int, arg int) int {
  return f(arg)
}

func main() {
  // 将 add 函数传递给 apply 函数
  result := apply(add, 10)

  fmt.Println(result) // 输出:11
}
Copy after login

3. Return function

func getAdder(val int) func(int) int {
  return func(arg int) int {
    return val + arg
  }
}

func main() {
  // 获得一个返回 10 加数的函数
  add10 := getAdder(10)

  // 使用 add10 函数
  fmt.Println(add10(20)) // 输出:30
}
Copy after login

Practical case: Function type to create sortable key-value pairs

The following is a Go language program that uses function pointers to create sortable key-value pairs:

type kv struct {
  key string
  val int
}

func (kv *kv) SortByValue() {
  sort.Slice(kv, func(i, j int) bool {
    return kv[i].val < kv[j].val
  })
}

func main() {
  kvList := []*kv{
    {"key1", 10},
    {"key2", 5},
    {"key3", 15},
  }

  kvList.SortByValue()

  for _, kv := range kvList {
    fmt.Println(kv.key, kv.val)
  }
}
Copy after login

Output:

key2 5
key1 10
key3 15
Copy after login

In this example, the SortByValue function pointer defines a custom sorting rule that sorts the elements in the kv slice in ascending order of value.

The above is the detailed content of The essence and usage of golang function pointers. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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