Can we have function pointers in Go?

golang when to use pointers in structs
golang function pointer in struct
go pointers
golang function pointers
golang interface
golang modify struct in function
golang pointer receiver
golang map

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

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.

Pointers to a Function in Go, For example, if you're a bank, and you have a function that shows the You can pass the pointer to the function instead of the data, and the  You can pass the pointer to the function instead of the data, and the function can then alter the original variable in place. This is called passing by reference, because the value of the variable isn’t passed to the function, just its location. In this article, you will create and use pointers to share access to the memory space for a variable.

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.

Pointers, Similar to function main, we can declare a custom function, which can Since we have named return values in function declaration, GO will  As you can see, because we have a pointer to the address of the variable, we can modify its value, even within the scope of another function. If the value were not a pointer, this would not work: If the value were not a pointer, this would not work:

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

Understanding Pointers in Go, In this tutorial we will learn how pointers work in Go and we will also understand how Go pointers differ from The & operator is used to get the address of a variable. 8 we use the new function to create a pointer of type int . Prerequisite: Pointers in Go and Passing Pointers to Function Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. We can pass pointers to the function as well as return pointer from a function in Golang.

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

new() vs make(), GO version 1.8 typedef struct test { int c; int (*add)(int a, int b); } test; func You need to pass the C function pointer into a C function which then  In Go, when we pass a variable to a function, we are copying the box with the value into that function. This means that the new copy of that box has a different location in the memory. If we use a pointer instead of just a normal parameter, we can pass the original box into the function.

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

Pointers, get the a variable address and its value call the setVal() function and pass the a 's 3 years ago we started our Medium blog Guide that will Boost your Workflow. 4. Go Pointers dereferencing. Dereferencing a pointer means getting the value inside the address the pointer holds. If we have a memory address, we can dereference the pointer to that memory address to get the value inside it. Here is the same example showing the dereference operation using the star(*) operator.

Functions and Pointers – letslearngo, We'll show how pointers work in contrast to values with 2 functions: zeroval and an int parameter, so arguments will be passed to it by value. zeroval will get a  Pointers reference a location in memory where a value is stored rather than the value itself. (They point to something else) By using a pointer (*int) the zero function is able to modify the original variable. The * and & operators In Go a pointer is represented using the * (asterisk) character followed by the type of the stored value.

Understanding pointers in Go with examples, Go allows to pass parameters both by pointers (sometimes it's called by reference) a new copy of the variable is created and passed to called function or method. We do not have other option, but pass variable by value. Pointers in Go are easy and fun to learn. Some Go programming tasks are performed more easily with pointers, and other tasks, such as call by reference, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect Go programmer.

can we call c function pointer in CGO? · Issue #24291 · golang/go , Functions have their own l-value function type -- in this case, a function type that The other primary thing you can do with a function pointer is use it to actually call As you see, both the return type and parameters go inside angled brackets,​  It is perfectly legal for a function to return a pointer of a local variable. The Go compiler is intelligent enough and it will allocate this variable on the heap. package main import ( "fmt" ) func hello() *int { i := 5 return &i } func main() { d := hello() fmt.Println("Value of d", *d) } Run in playground.

Comments
  • I don't know enough about Go to answer your question, but I have a counterquestion: why would you want to have function pointers when Go has proper first-class functions?
  • I suppose it is more of an educational question. I have a background in C and just started learning about Go. I noticed that Go has pointers similar to C and so wanted to learn if function pointers are possible in Go and if yes, how to declare them.
  • I think you are misreading that blog post. It is not saying that function pointers aren't "allowed", rather that they have a different declaration syntax to in C.
  • I'd say this is a complicated answer for a question that has a simple solution.
  • Furthermore, instead of making a struct that contains a string, just create a new type that IS a string: type Delete string and then define your interface on that new type. Then going from string to Delete is just a type cast: Delete("new"). And vice-versa.