logo-MindMajix
PostGoLangGoLang
c
cayrel rings

Posted on 23rd June 2024|85 views

0
votes

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

}

 

1 answers
Answers
P
naveen nani

Posted on 23rd June 2024| views

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

STILL GOT QUERIES?

Get a Live FREE Demo

  • 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
Name
Course *
Email *
Phone Number