Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: function pointer to function with receiver

Tags:

go

Can I set function pointer to function with receiver simpler than creating function around it?

package main

import "fmt"

type hello struct {
  name string
}

func (obj *hello) hello() {
  fmt.Printf("Hello %s\n", obj.name)
}

func ntimes(action func (), n int) {
  for i := 0; i < n; i++ {
    action()
  }
}

func main() {
  obj := hello{"world"}
  // Can I do following simpler?
  ntimes(func() {obj.hello();}, 3)
}
like image 391
demi Avatar asked Jul 19 '26 17:07

demi


1 Answers

Not right now. But with Go 1.1 this will be possible. Go 1.1 Function Calls

Go 1.1 will be ready when the blue line touches zero.

like image 186
Kocka Avatar answered Jul 22 '26 10:07

Kocka