在Go语言中,函数变量的类型是”func”。

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

Type of a function variable in Go

问题

我想编写一个函数,该函数接受任何类型的函数指针作为参数。我可以这样做:

func myFunc(f interface{})

...但这样会允许非函数值。有没有办法限制参数类型为任何函数?

英文:

I want to write a function which takes a pointer to any type of function. I could do:

func myFunc(f interface{})

...but that would allow non-function values. Is there any way I can restrict the type to any function?

答案1

得分: 5

假设你字面上的意思是任何函数,你可以使用类型切换(这将是具体的):

switch v.(type) {
case func() int:
case func() string:
}

或者你可以使用reflect包来确定类型:

if reflect.TypeOf(v).Kind() != reflect.Func {
    // 这里有错误
}

这是一种运行时的解决方案。除此之外,你无法做其他事情。这种方法的缺点是编译器无法阻止将非函数值传递给它。

个人而言,我会避免这样做,并且我希望有一个特定的函数原型,比如:

func myFunc(f func() string)

这样,当编译器知道类型时,你就不太可能出现错误。

英文:

Assuming you literally mean any function, you can either do a type switch (which would be specific):

switch v.(type) {
case func() int:
case func() string:
}

Or you could use the reflect package to determine the type:

if reflect.TypeOf(v).Kind() != reflect.Func {
    // error here
}

This is a runtime solution. Other than that, there's nothing else you can do. The downside about this is the compiler won't stop someone from passing a non-function value.

Personally I would avoid doing this, and I would expect a specific func prototype, like:

func myFunc(f func() string)

With this you're less likely to have errors when the compiler knows what the types are.

答案2

得分: 1

"任何函数"的概念在Go语言中可以通过两个步骤来实现:

type MotherOfAllFuncs func()

下一步是将任何函数封装在一个闭包中,作为一个匿名类型,可以赋值给MotherOfAllFuncs

func() {
        anyFunction(with, any, parameters)
}

package main

import "fmt"

type f func()

func g(f f) {
        f()
}

func h(i int) {
        fmt.Println(21 * i)
}

func i() {
        fmt.Println(314)
}

func main() {
        g(func() { h(2) })
        g(func() { i() })
}

Playground


输出:

42
314
英文:

The concept of "any function" can be constructed in Go in two steps:

type MotherOfAllFuncs func()

The next step is to to wrap any function in a closure, which, as an anonymous type, is assignment compatible with MotherOfAllFuncs.

func() {
        anyFunction(with, any, parameters)
}

package main

import "fmt"

type f func()

func g(f f) {
        f()
}

func h(i int) {
        fmt.Println(21 * i)
}

func i() {
        fmt.Println(314)
}

func main() {
        g(func() { h(2) })
        g(func() { i() })
}

Playground


Output:

42
314

答案3

得分: 0

函数在Go语言中是一等公民(first class citizens)。因此,它们可以像其他类型一样作为参数传递给函数,也可以从函数中返回。

你可以定义一个函数,使其接受函数作为参数。

了解更多信息 - 一等函数Go语言中的一等函数

英文:

Functions are first class citizens in Go. So they can be passed to and returned from functions like any other type.

You can define your function to take function as its argument.

Read more - First class functions and First class functions in Go.

huangapple
  • 本文由 发表于 2013年8月7日 07:35:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/18092276.html
匿名

发表评论

匿名网友

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

确定