golang反映得到关闭函数指针 [英] golang reflect get closure function pointer

查看:95
本文介绍了golang反映得到关闭函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  package main 
import(
fmt
反映

func main(){

工厂:= func(name string)func(){
return func(){
fmt .Println(名称)
}
}
f1:=工厂(f1)
f2:=工厂(f2)

pf1 := reflect.ValueOf(f1)
pf2:= reflect.ValueOf(f2)

fmt.Println(pf1.Pointer(),pf2.Pointer())
fmt .Println(pf1.Pointer()== pf2.Pointer())

f1()
f2()
}



结果:

  4199328 4199328 
true
f1
f2

为什么要得到封闭的地址功能!
或如何得到一个唯一的地址!

解决方案

函数指针表示函数的代码。而函数文字创建的匿名函数的代码只会在内存中存储一​​次,而不管返回匿名函数值的代码运行多少次。这意味着所有的函数值(或更确切地说是函数指针)将是相同的。



所以你不能区分存储在 f1 f2 :它们表示在调用它们时要执行的代码块。



存储在工厂变量中的函数值返回的函数值是闭包。只要可访问,它所引用的环境(局部变量和函数参数)就会存活。在你的情况下,这意味着由于 f1 f2 中的函数值引用名称>的参数,只要函数值(在 f1 f2 )可以访问。这是唯一让他们不同或独特的东西,但这是从外部看不见的。



您会继续打印 name ,你会发现它们是闭包的多个值的不同变量,但是相同如果再次调用相同的闭包(函数值)。首先修改闭包以打印名称的地址:

 工厂:= func(name string)func(){
return func(){
fmt.Println(name,& name)
}
}

然后调用 f1 f2 多次:

  f1()
f2()
f1()
f2()

输出:

 f1 0x1040a120 
f2 0x1040a130
f1 0x1040a120
f2 0x1040a130

正如您所看到的,名称参数被保留,并且相同的名称参数如果再次调用相同的闭包,将再次使用它。


please review code

package main
import (
    "fmt"
    "reflect"
)
func main() {

    factory := func  (name string) func(){
        return func (){
            fmt.Println(name)
        }
    }
    f1 := factory("f1")
    f2 := factory("f2")

    pf1 := reflect.ValueOf(f1)
    pf2 := reflect.ValueOf(f2)

    fmt.Println(pf1.Pointer(), pf2.Pointer())
    fmt.Println(pf1.Pointer() == pf2.Pointer())

    f1()
    f2()
}

the result:

4199328 4199328
true
f1
f2

Why get to the same address of the closure function! Or to how to get a unique address!

解决方案

Function pointers denote the code of the function. And the code of an anonymous function created by function literal is only stored once in memory, no matter how many times the code that returns the anonymous function value runs. This means all function values (or more precisely the function pointers) will be the same.

So you can't distinguish between the values stored in f1 and f2: they denote the same code block to be executed when they are called.

The function value that is returned by the function value stored in your factory variable is a closure. The environment (local variables and function parameters) referred by it survives as long as it is accessible. In your case this means since the function values in f1 and f2 refer to the name argument of the enclosing anonymous function, they ("they" as from multiple calls) will be retained as long as the function values (in f1 and f2) are accessible. This is the only thing that makes them "different" or unique, but this is invisible from the "outside".

Would you go ahead and print the address of name in the closure, you'd see they are different variables for multiple values of the closure, but it is the same if the same closure (function value) is called again. First modify the closure to also print the address of name:

factory := func(name string) func() {
    return func() {
        fmt.Println(name, &name)
    }
}

And call f1 and f2 multiple times:

f1()
f2()
f1()
f2()

Output:

f1 0x1040a120
f2 0x1040a130
f1 0x1040a120
f2 0x1040a130

As you can see, the name argument is retained and the same name argument is used again if the same closure is invoked again.

这篇关于golang反映得到关闭函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆