1

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.

Tarion
  • 16,283
  • 13
  • 71
  • 107
  • 2
    See [Address Operators](https://golang.org/ref/spec#Address_operators) in the spec. `The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array` – JimB Dec 08 '16 at 15:52
  • 4
    Since there's no such thing as an address to a function in Go, what is it you're actually trying to do? – JimB Dec 08 '16 at 15:53
  • 1
    Possible duplicate of [golang reflect get closure function pointer](http://stackoverflow.com/questions/36126552/golang-reflect-get-closure-function-pointer/36127142#36127142); and [How to compare 2 functions in Go?](http://stackoverflow.com/questions/34901307/how-to-compare-2-functions-in-go/34901677#34901677) – icza Dec 08 '16 at 16:05
  • My questions is a little related to CGO where you can set at least a C function pointer to the value of the Exported Go function. I will add this to the question. – Tarion Dec 09 '16 at 09:22

4 Answers4

9

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.

Community
  • 1
  • 1
I159
  • 29,741
  • 31
  • 97
  • 132
3

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

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)
}
  • what does `fmt.Println(HelloWorld)` mean? It also print an address, but not the same to your solution. @Siu Ching Pong -Asuka Kenji- – river Jan 22 '19 at 09:57
0

You can get address of function use function 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)

   //Thits is not are functon addrress!!!
   BadPTR1 := reflect.ValueOf(HelloWorld).Pointer()
   BadPTR2 := **(**uintptr)(unsafe.Pointer(&tmp)) //dereferenced pointer
   fmt.Printf("\nBAD: 0x%x = 0x%x", BadPTR1 , BadPTR2 )
  }
Egor
  • 41
  • 3
  • 1
    Code dumps do not make for good answers. You should explain *how* and *why* this solves their problem. I recommend reading, "[How do I write a good answer?"](//stackoverflow.com/help/how-to-answer). This can help future users learn and eventually apply that knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – John Conde Feb 27 '21 at 00:25
0

You can do this:

myFunction := func() {}
fmt.Println(&myFunction)
ceving
  • 21,900
  • 13
  • 104
  • 178