tags:

views:

180

answers:

3

I was learning about pointers in Google 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);

+1  A: 

It might be possible, although my googling didn't reveal any obvious mechanism for it. It would, however, be a really, really bad idea. You have first-class functions that interact nicely with the type system. They can do everything that function pointers can do except for causing horrendous crashes.

I notice your code comment says that "pfunc is a pointer to the function 'hello'". This is the wrong way to think about first-class functions, even if that might be the implementation mechanism behind the scenes. What that statement really says is "pfunc holds the value that is the function 'hello'". Functions are values just like integers or strings are!

Gian
Thanks Gian. So I see that assigning a function to a variable in Go, assigns it as a value.And it seems there might be no need to use function pointers (as suggested there are better mechanisms).
Kevin
Yeah. Function pointers tend to be used to emulate functions-as-first-class-values functionality, which Go actually implements, so I would suggest that there is absolutely no need for function pointers. Anything you might do by arithmetic on function pointers (!!!) is probably achievable using higher-order functions (i.e. functions that return functions). Behind the scenes there might be function pointers used to implement all this, but the nice thing about modern languages is that you can just not think too much about that and rely on compilers to "do the right thing" in all cases.
Gian
+6  A: 

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 an own type.

Mue
Thanks Mue for explaining usage of the "type" for functions.
Kevin
A: 

Go doesn't allow function pointers in the same way C and C++ do. There's a pretty good explanation for that on the Go blog.

Here's an example I wrote. Notice how the fp parameter is defined in calculate().

package main

import "fmt"

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) 
}

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 f parameter is defined as as function that takes two ints and returns a single int. This is somewhat the same thing Mue mentioned but shows a different usage example.

Hannson
Thanks Hannson.
Kevin