Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we have function pointers in Go?

Tags:

go

I was learning about pointers in Go. And managed to write something like:

func hello(){

       fmt.Println("Hello World")
}

func main(){

       pfunc := hello     //pfunc is a pointer to the function "hello"
       pfunc()            //calling pfunc prints "Hello World" similar to hello function
}

Is there a way to declare the function pointer without defining it as done above? Can we write something like we do in C?

e.g. void (*pfunc)(void);

like image 316
Kevin Avatar asked Aug 30 '10 15:08

Kevin


People also ask

Does Go have function pointers?

Go pointers store the memory addresses of variables. And just like regular variables, we can also pass pointers to functions.

How do you send a pointer to a function in Go?

Go programming language allows you to pass a pointer to a function. To do so, simply declare the function parameter as a pointer type.

Does Go have pointer to pointer?

Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. A pointer is a special variable so it can point to a variable of any type even to a pointer. Basically, this looks like a chain of pointers.

What types of pointers does Go have?

The pointer type in Go is used to point to a memory address where data is stored. Similar to C/C++, Go uses the * operator to designate a type as a pointer. The following snippet shows several pointers with different underlying types.


3 Answers

It works if you're using the signature. There's no pointer.

type HelloFunc func(string)  func SayHello(to string) {     fmt.Printf("Hello, %s!\n", to) }  func main() {     var hf HelloFunc      hf = SayHello      hf("world") } 

Alternatively you can use the function signature directly, without declaring a new type.

like image 120
themue Avatar answered Sep 23 '22 00:09

themue


Go doesn't have the same syntax for function pointers as C and C++ do. There's a pretty good explanation for that on the Go blog. Understandably the Go authors thought C's syntax for function pointers too similar to regular pointers, so in short they decided to make function pointers explicit; i.e. more readable.

Here's an example I wrote. Notice how the fp parameter is defined in calculate() and the other example below that shows you how you can make a function pointer into a type and use it in a function (the commented calculate function).

package main  import "fmt"  type ArithOp func(int, int)int  func main() {     calculate(Plus)     calculate(Minus)     calculate(Multiply) }  func calculate(fp func(int, int)int) {     ans := fp(3,2)     fmt.Printf("\n%v\n", ans)  }  // This is the same function but uses the type/fp defined above //  // func calculate (fp ArithOp) { //     ans := fp(3,2) //     fmt.Printf("\n%v\n", ans)  // }  func Plus(a, b int) int {     return a + b }  func Minus(a, b int) int {     return a - b }  func Multiply(a,b int) int {     return a * b } 

The fp parameter is defined as a function that takes two ints and returns a single int. This is somewhat the same thing Mue mentioned but shows a different usage example.

like image 31
hannson Avatar answered Sep 25 '22 00:09

hannson


A function is also a type in Go. So you can essentially create a variable of type func signature. So following would work;

var pfunc func(string)

This variable can point to any function that take string as argument and returns nothing. Following piece of code works well.

package main

import "fmt"

func SayHello(to string) {
    fmt.Printf("Hello, %s!\n", to)
}

func main() {
    var pfunc func(string)

    pfunc = SayHello

    pfunc("world")
}
like image 37
Devendra Mukharaiya Avatar answered Sep 24 '22 00:09

Devendra Mukharaiya