44 Views

3rd August 2020

Golang Function Pointer

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

}

 

Answers

8th August 2020

Answered by naveen nani

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

 

Write your answer

GET STARTED

Speak with a Learning Advisor

Understand how the industry and practically oriented courses at Mindmajix transform your career.

  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos