golang函数以”type ***”开头

huangapple go评论75阅读模式
英文:

golang function with leading "type ***"

问题

是的,你的理解是正确的。这个声明定义了一个函数指针类型叫做ApplyFunc。这个函数接受commitIndex和cmd作为输入,并返回[]byte类型的结果。

英文:
type ApplyFunc func(commitIndex uint64, cmd []byte) []byte

For this declaration. My understanding is,this is a function pointer. Its name is ApplyFunc.
And this function takes commitIndex and cmd as inputs. It returns []byte.

Is my understanding right?
thanks!

答案1

得分: 7

Golang函数是一等公民,可以在任何需要func(commitIndex uint64, cmd []byte) []byte类型的地方使用ApplyFunc,具体可以参考go-example页面

它是一个**命名类型**,这意味着你可以在任何需要func(commitIndex uint64, cmd []byte) []byte类型的地方使用ApplyFunc,详见"Golang:为什么我可以给函数起别名并在不进行强制类型转换的情况下使用它们?"。

正如Volker所评论的那样,它不是一个函数或者"指向函数的指针"。它是一种类型,允许你声明一个变量来存储任何与其声明类型具有相同函数签名的函数,就像一个函数字面量(或者"匿名函数")。

var af ApplyFunc = func(uint64, []byte) []byte {return nil}
                 // (函数字面量或者"匿名函数")

参考"匿名函数和闭包":你可以定义一个返回另一个函数的函数,利用闭包的特性:

函数字面量是闭包:它们可以引用在外部函数中定义的变量。这些变量在外部函数和函数字面量之间共享,并且只要它们可访问,它们就会一直存在。

(参见playground示例

type inc func(digit int) int

func getIncbynFunction(n int) inc {
	return func(value int) int {
		return value + n
	}
}

func main() {
	g := getIncbynFunction
	h := g(4)
	i := g(6)
	fmt.Println(h(5)) // 返回5+4,因为n被设置为4
	fmt.Println(i(1)) // 返回1+6,因为n被设置为6
}

此外,正如"Golang函数指针作为结构体的一部分"所示,你可以在ApplyFunc的函数接收器上定义函数。

英文:

Golang function are first-class, as illustrated in this go-example page.

It is a named type, which means you can use ApplyFunc anywhere where a func(commitIndex uint64, cmd []byte) []byte is expected: see "Golang: Why can I type alias functions and use them without casting?".

It means, as commented by Volker, it isn't a function or a "pointer to a function".
It is a type which allows you to declare a variable storing any function which respects the same func signature as its declared type, like a function literal (or "anonymous function").

var af ApplyFunc = func(uint64,[]byte) []byte {return nil}
                 // (function literal or "anonymous function")

See "Anonymous Functions and Closures": you can define a function which returns another function, taking advantage of closure:

> Function literals are closures: they may refer to variables defined in a surrounding function.
Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible.

(see playground example)

type inc func(digit int) int

func getIncbynFunction(n int) inc {
	return func(value int) int {
		return value + n
	}
}

func main() {
	g := getIncbynFunction
	h := g(4)
	i := g(6)
	fmt.Println(h(5)) // return 5+4, since n has been set to 4
	fmt.Println(i(1)) // return 1+6, since n has been set to 6
}

Also, As illustrated in "Golang function pointer as a part of a struct", you can define functions on a func receiver ApplyFunc(!).

huangapple
  • 本文由 发表于 2014年8月26日 13:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/25498505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定