Post
GoLang
Can I have a function pointer in Go?
I am very new to Go, so I just want to know is there any way to declare the function pointer as we do in C like this void (*pfunc)(void); here is my code
func hey(){
fmt.printil("hey")
}
func main(){
pfunc := hey
pfunc()
}
Posted on 25th April 2024
A function in Go is a type, so you can actually create a variable of type func, signature this may work
var pfunc func(string)
This variable can point to a function which takes a string as an argument also returns nothing. This example works well.
package main
import "fmt"
func hey(to string)
{ fmt.Printf("Hey, %s!\n", to) }
func main()
{ var pfunc func(string)
pfunc = hey
pfunc("world") }
STILL GOT QUERIES?