如何在Go语言中获取函数的地址?

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

How to get the address of a function in go?

问题

在Go语言中,获取函数引用的地址是不被支持的。你无法像你提供的代码那样直接获取函数引用的地址。根据我的了解,这是不可能的,但我还没有找到确凿的证据。

背景说明:
你的问题背景涉及到使用CGO和C函数指针。你提供的代码中展示了一种在Go和C之间传递函数指针的方法。尽管文档中指出传递Go函数的指针是不可行的,但你的代码似乎离这个目标并不远。我只是想知道是否有办法跳过导出步骤。

英文:

Is it possible to get the address of a function reference in Go?

Something like

func myFunction() {
}

// ...

unsafe.Pointer(&myFunction)

Just that is does not work that way. My guess it's not possible, but I did not found any proof yet.

Edit: Background

The background of my question comes from dealing with CGO and C Function pointers.
This works:

/*
void go_myFunction();

typedef void (*myFunction_f)();

myFunction_f pMyFunction;
*/
import "C"

//export go_myFunction
func go_myFunction() {
// ...
}

func SetupFp() {
  C.pMyFunction = (*[0]byte)(unsafe.Pointer(C.go_myFunction))
}

I'm also aware that the documentation states that passing a pointer to a go function does not work. But the above code seems no to be that far from it. I was just wondering if one could somehow skip the export step.

答案1

得分: 9

在Go语言中,function类型既不可寻址也不可比较,原因如下:

函数指针表示函数的代码。通过函数字面量创建的匿名函数的代码只会在内存中存储一次,无论返回匿名函数值的代码运行多少次。

如果你需要比较函数的地址,可以使用reflect.Pointer。但是,这个操作更多是没有意义的,因为它几乎是不可能的,原因如下:

如果v的类型是Func,返回的指针是底层代码指针,但不一定足以唯一标识单个函数。唯一的保证是,当且仅当v是nil func Value时,结果为零。

英文:

function type in Go is not addressable and not comparable because:

> 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.

Original answer

If you need to compare addresses of a functions you can do it with reflect.Pointer. But any way this operation is more senseless than impossible because:

> If v's Kind is Func, the returned pointer is an underlying code pointer, but not necessarily enough to identify a single function uniquely. The only guarantee is that the result is zero if and only if v is a nil func Value.

答案2

得分: 3

你可以通过以下方式获取Go函数的地址:

package main

import (
	"fmt"
	"reflect"
)

func HelloWorld() {
	fmt.Println("Hello, world!")
}

func main() {
	var ptr uintptr = reflect.ValueOf(HelloWorld).Pointer()
	fmt.Printf("0x%x", ptr)
}

这段代码可以打印出HelloWorld函数的地址。

英文:

You may get the address of a Go function like this:

<!-- language: lang-golang -->

package main

import (
	&quot;fmt&quot;
	&quot;reflect&quot;
)

func HelloWorld() {
	fmt.Println(&quot;Hello, world!&quot;)
}

func main() {
	var ptr uintptr = reflect.ValueOf(HelloWorld).Pointer()
	fmt.Printf(&quot;0x%x&quot;, ptr)
}

答案3

得分: 0

你可以使用函数GetFuncAddr来获取函数的地址:

package main

import (
    "fmt"
    "unsafe"
    "reflect"
)

func HelloWorld() {
    fmt.Println("Hello, world!")
}


func GetFuncAddr(i interface{}) uintptr {
    type IHeader struct {
        typ  uintptr
        word uintptr
    }
    
    return (*IHeader)(unsafe.Pointer(&i)).word
}

func main() {
    tmp := HelloWorld
    ptr1 := *(*uintptr)(unsafe.Pointer(&tmp)) //Way 1

    ptr2 := GetFuncAddr(HelloWorld)  //Way 2
    fmt.Printf("0x%x = 0x%x", ptr1, ptr2)

    //This is not a function address!!!
    BadPTR1 := reflect.ValueOf(HelloWorld).Pointer()
    BadPTR2 := **(**uintptr)(unsafe.Pointer(&tmp)) //dereferenced pointer
    fmt.Printf("\nBAD: 0x%x = 0x%x", BadPTR1 , BadPTR2 )
}

你可以使用函数GetFuncAddr来获取函数的地址。在上面的代码中,函数HelloWorld打印"Hello, world!"。函数GetFuncAddr接受一个接口类型的参数,并返回函数的地址。在main函数中,我们使用两种方式来获取HelloWorld函数的地址,并将结果打印出来。请注意,代码中还包含了一些注释,用于说明一些细节。

英文:

You can get address of function use function GetFuncAddr:

    package main

     import (
         &quot;fmt&quot;
         &quot;unsafe&quot;
         &quot;reflect&quot;
      )

     func HelloWorld() {
        fmt.Println(&quot;Hello, world!&quot;)
     }


     func GetFuncAddr(i interface{}) uintptr {
     type IHeader struct {
        typ  uintptr
        word uintptr
      }
    
    return (*IHeader)(unsafe.Pointer(&amp;i)).word
    }

  func main() {
   tmp := HelloWorld
   ptr1 := *(*uintptr)(unsafe.Pointer(&amp;tmp)) //Way 1

   ptr2 := GetFuncAddr(HelloWorld)  //Way 2
   fmt.Printf(&quot;0x%x = 0x%x&quot;, ptr1, ptr2)

   //Thits is not are functon addrress!!!
   BadPTR1 := reflect.ValueOf(HelloWorld).Pointer()
   BadPTR2 := **(**uintptr)(unsafe.Pointer(&amp;tmp)) //dereferenced pointer
   fmt.Printf(&quot;\nBAD: 0x%x = 0x%x&quot;, BadPTR1 , BadPTR2 )
  }

答案4

得分: 0

你可以这样做:

myFunction := func() {}
fmt.Println(&myFunction)
英文:

You can do this:

myFunction := func() {}
fmt.Println(&amp;myFunction)

huangapple
  • 本文由 发表于 2016年12月8日 23:49:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/41043462.html
匿名

发表评论

匿名网友

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

确定