Can we have function pointers in Go?

Go

Go Problem Overview


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

Go Solutions


Solution 1 - Go

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.

Solution 2 - Go

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.

Solution 3 - Go

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

Solution 4 - Go

You could do it like this:

package main

import "fmt"

func hello(){

       fmt.Println("Hello World")
}

func main(){
       var pfunc func()
       pfunc = hello     //pfunc is a pointer to the function "hello"
       pfunc()            
}

If your function has arguments and e.g. a return value, it would look like:

func hello(name string) int{

       fmt.Println("Hello %s", name)
       return 0
}

and the variable would look like:

  var pfunc func(string)int

Solution 5 - Go

Array based function pointer solution

package main

import (
	"fmt"
)

type pfunc func(string, int)int

func testCase1(toStr string, toInt int)int {
	fmt.Printf("1st Function Call %s %d\n",toStr, toInt)
	return toInt
}

func testCase2(toStr string, toInt int)int {
	fmt.Printf("2nd Function Call %s %d\n",toStr, toInt)
	return toInt
}

func testCase3(toStr string, toInt int)int {
	fmt.Printf("3rd Function Call %s %d\n",toStr, toInt)
	return toInt
}

func main() {
		
	funcArray := []pfunc{testCase1,testCase2,testCase3}	
	
	for n:=range funcArray {
		result := funcArray[n]("Test", n)
		fmt.Printf("Run Test Case #%d reference %v result %d\n",n, funcArray[n], result)
	}
}

Solution 6 - Go

A different way to approach it is to define an interface

type command interface {
      DoLoop()
}

implement a struct that implements it

type Delete struct {
      instance string
}

func (dev Delete) DoLoop() {
      fmt.Println("input: delete ")
}

Create map that contains the struct

 mainFuncTable = make(map[string]command)
 mainFuncTable["delete"] = Delete{"new"}

the call the function

func route(command string) {
      cmd := mainFuncTable[command]
      cmd.DoLoop()
}

It's a little indirect but it works

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKevinView Question on Stackoverflow
Solution 1 - GothemueView Answer on Stackoverflow
Solution 2 - GohannsonView Answer on Stackoverflow
Solution 3 - GoDevendra MukharaiyaView Answer on Stackoverflow
Solution 4 - GonosView Answer on Stackoverflow
Solution 5 - GoMinView Answer on Stackoverflow
Solution 6 - GoDon NewtonView Answer on Stackoverflow