Passing a function with parameters to time.AfterFunc - go

time.AfterFunc() accepts a duration and a function to be executed when that duration has expired. But the function cannot be a function that accepts parameters.
For example: The following function cannot be passed:
func Foo (b *Bar) {}
Although, it is possible to initialize a new function that calls the above one and then pass it:
f := func() {
Foo(somebar)
}
timer := time.AfterFunc(1*time.Second, f)
Should this really be done this way?
Why does time.AfterFunc not accept any functions that accept parameters?
Do there exist other/better ways to do this?

Create a function from the argument, return it.
package main
import (
"fmt"
"time"
)
func foo(bar string) {
fmt.Printf("in foo('%s')\n", bar)
}
func newFunc(bar string) func() {
fmt.Printf("creating func with '%s'\n", bar)
return func() {
foo(bar)
}
}
func main() {
somebar := "Here we go!"
f := newFunc(somebar)
_ = time.AfterFunc(1*time.Second, f)
time.Sleep(2 * time.Second)
}
https://play.golang.com/p/lWgeHvPLg9N

Anonymous function helps you to send functions with parameters to AfterFunc.
package main
import (
"fmt"
"time"
)
func foo(bar string) {
fmt.Printf("in foo('%s')\n", bar)
}
func main() {
somebar := "Here we go!"
time.AfterFunc(1*time.Second, func(){foo(somebar)})
time.Sleep(2 * time.Second)
}
https://play.golang.com/p/sdpiBtBWt_s

Related

How to pass parameters to decorated functions?

I would like to write a decorator to wrap a function with "before" and "after" commands. A first version is below, where the decorated function just outputs hello:
package main
import "fmt"
func main() {
wrapper(printHello, "world")
}
func wrapper(f func(), who string) {
fmt.Printf("before function, sending %v\n", who)
f()
fmt.Print("after function\n")
}
func printHello() {
fmt.Printf("hello\n")
}
(Playground: https://play.golang.org/p/vJuQKWpZ2h9)
I now would like to call the decorated function with a parameter (in my case "world"). In the example above, it is successfully passed to wrapper() but then I do not know what to do further. I thought that I would just
package main
import "fmt"
func main() {
wrapper(printHello, "world") // cannot use printHello as the type func()
}
func wrapper(f func(), who string) {
fmt.Printf("before function, sending %v\n", who)
f(who) // too many arguments
fmt.Print("after function\n")
}
func printHello(who string) {
fmt.Printf("hello %v\n", who)
}
The compilation failes with
.\scratch_11.go:6:9: cannot use printHello (type func(string)) as type func() in argument to wrapper
.\scratch_11.go:11:3: too many arguments in call to f
have (string)
want ()
What is the proper way to pass arguments to the decorated function?
You have to declare the correct variable type for this to work:
func wrapper(f func(string), who string) {
...

gocron to add and remove task dynamically with parameter

I am using gocron in my current project and I had encounter a few situations that not in the document.
I test this code:
gocron.Every(3).Seconds().Do(taskWithParams,2,"world")
gocron.Every(2).Seconds().Do(taskWithParams,1, "hello")
gocron.Start()
time.Sleep(10 * time.Second)
gocron.Remove(taskWithParams)//<-- remove task
...
func taskWithParams(a int, b string) {
fmt.Println(a, b)
}
When I remove task(gocron.Remove(taskWithParams)), always gocron.Every(3).Seconds().Do(taskWithParams,2,"world") is removed. even I swap them:
gocron.Every(2).Seconds().Do(taskWithParams,1, "hello")
gocron.Every(3).Seconds().Do(taskWithParams,2,"world")
Is there a way for me to specifically point out which task I want to remove, since the remove() only allow 1 argument?
The document also have a scheduler:
s := gocron.NewScheduler()
s.Every(3).Seconds().Do(task)
<- s.Start()
When is the best use case for scheduler?
If we are done with scheduler, how to remove it from memory? do scheduler.Clear() does the job? or we have to have another way to clear them from the memory?
you can handle the removal logic by deduplicating the function handlers.
package main
import (
"fmt"
)
func main() {
fn1 := func() { taskWithParams(2, "world") }
gocron.Every(3).Seconds().Do(fn1)
fn2 := func() { taskWithParams(1, "hello") }
gocron.Every(2).Seconds().Do(fn2)
gocron.Start()
time.Sleep(10 * time.Second)
gocron.Remove(fn2)
}
func taskWithParams(a int, b string) {
fmt.Println(a, b)
}
Otherwise, the scheduler.Do method returns an instance of *Job that you can pass to scheduler.RemoveByReference.
package main
import (
"fmt"
)
func main() {
job, err := gocron.Every(3).Seconds().Do(taskWithParams, 2, "ww")
if err != nil {
panic(err)
}
gocron.Every(2).Seconds().Do(taskWithParams, 1, "hh")
gocron.Start()
time.Sleep(10 * time.Second)
gocron.RemoveByReference(job)
}
func taskWithParams(a int, b string) {
fmt.Println(a, b)
}

Is it possible get information about caller function in Golang?

Is it possible get information about caller function in Golang? For example if I have
func foo() {
//Do something
}
func main() {
foo()
}
How can I get that foo has been called from main?
I'm able to this in other language (for example in C# I just need to use CallerMemberName class attribute)
You can use runtime.Caller for easily retrieving information about the caller:
func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Example #1: Print caller file name and line number: https://play.golang.org/p/cdO4Z4ApHS
package main
import (
"fmt"
"runtime"
)
func foo() {
_, file, no, ok := runtime.Caller(1)
if ok {
fmt.Printf("called from %s#%d\n", file, no)
}
}
func main() {
foo()
}
Example #2: Get more information with runtime.FuncForPC: https://play.golang.org/p/y8mpQq2mAv
package main
import (
"fmt"
"runtime"
)
func foo() {
pc, _, _, ok := runtime.Caller(1)
details := runtime.FuncForPC(pc)
if ok && details != nil {
fmt.Printf("called from %s\n", details.Name())
}
}
func main() {
foo()
}
expanding on my comment, here's some code that returns the current func's caller
import(
"fmt"
"runtime"
)
func getFrame(skipFrames int) runtime.Frame {
// We need the frame at index skipFrames+2, since we never want runtime.Callers and getFrame
targetFrameIndex := skipFrames + 2
// Set size to targetFrameIndex+2 to ensure we have room for one more caller than we need
programCounters := make([]uintptr, targetFrameIndex+2)
n := runtime.Callers(0, programCounters)
frame := runtime.Frame{Function: "unknown"}
if n > 0 {
frames := runtime.CallersFrames(programCounters[:n])
for more, frameIndex := true, 0; more && frameIndex <= targetFrameIndex; frameIndex++ {
var frameCandidate runtime.Frame
frameCandidate, more = frames.Next()
if frameIndex == targetFrameIndex {
frame = frameCandidate
}
}
}
return frame
}
// MyCaller returns the caller of the function that called it :)
func MyCaller() string {
// Skip GetCallerFunctionName and the function to get the caller of
return getFrame(2).Function
}
// foo calls MyCaller
func foo() {
fmt.Println(MyCaller())
}
// bar is what we want to see in the output - it is our "caller"
func bar() {
foo()
}
func main(){
bar()
}
For more examples: https://play.golang.org/p/cv-SpkvexuM

GO: find/scan for structs/functions

Is it possible in GO to find structs or functions by criteria such as name, tag or interface? i.e something along the lines of command line tasks/verbs? i.e:
func cmd1() {
...
}
func cmd2() {
...
}
...
func cmdN() {
}
func main() {
// Inspect os.Args and call cmd{X}() based on args.
...
}
I don't mind what the exact mechanism is and if the final targets are functions or structs - the goal is to get something working by convention without any boilerplate code.
You could use reflection
package main
import (
"flag"
"fmt"
"reflect"
)
var cmd command
type command struct{}
func (c command) execute(name string) {
v := reflect.ValueOf(c)
cmd := v.MethodByName(name)
if !cmd.IsValid() {
fmt.Println(name + " not a command")
return
}
cmd.Call([]reflect.Value{})
}
func (c command) Cmd1() {
fmt.Println("command 1")
}
func (c command) Cmd2() {
fmt.Println("command 2")
}
func (c command) Cmd3() {
fmt.Println("command 3")
}
func main() {
flag.Parse()
cmd.execute(flag.Arg(0))
}
or you could use a map.
package main
import (
"flag"
"fmt"
)
func cmd1() {
fmt.Println("command 1")
}
func cmd2() {
fmt.Println("command 2")
}
func cmd3() {
fmt.Println("command 3")
}
var funcs = map[string]func(){
"cmd1": cmd1,
"cmd2": cmd2,
"cmd3": cmd3,
}
func main() {
flag.Parse()
if f, ok := funcs[flag.Arg(0)]; ok {
f()
} else {
fmt.Println(flag.Arg(0) + " command not found")
}
}
I used a similar approach in "How to test a collection of functions by reflection in Go?"
The idea is to list and find all the functions needed, in my case, functions for a certain struct type:
stype := reflect.ValueOf(s)
for _, fname := range funcNames {
sfunc := stype.MethodByName(fname)
// no parameter => empty slice of Value
ret := sfunc.Call([]reflect.Value{})

How to define a function type which accepts any number of arguments in Go?

I try to write a function which takes any other function and wraps a new function around it. This is what I have tried so far:
package main
import (
"fmt"
)
func protect (unprotected func (...interface{})) (func (...interface{})) {
return func (args ...interface{}) {
fmt.Println ("protected");
unprotected (args...);
};
}
func main () {
a := func () {
fmt.Println ("unprotected");
};
b := protect (a);
b ();
}
When I compile this I get the error:
cannot use a (type func()) as type func(...interface { }) in function argument
Why is a function without arguments not compatible to a function with a variable number of arguments? What can I do to make them compatible?
Update:
The protected function should be compatible with the original:
func take_func_int_int (f func (x int) (y int)) (int) {
return f (1)
}
func main () {
a := func (x int) (y int) {
return 2 * x
}
b := protect (a)
take_func_int_int (a)
take_func_int_int (b)
}
Types are pretty concrete in Go. You could try
a := func(_ ...interface{}) {
fmt.Println("unprotected")
}
func (...interface{}) does not mean "any function that takes any number of any kind of arguments", it means "only a function which takes a variable number of interface{} arguments"
Alternatively rather than func(...interface{}) you can just use interface{} and the reflect package. See http://github.com/hoisie/web.go for an example.
EDIT: Specifically, this:
package main
import (
"fmt"
"reflect"
)
func protect(oldfunc interface{}) (func (...interface{})) {
if reflect.TypeOf(oldfunc).Kind() != reflect.Func {
panic("protected item is not a function")
}
return func (args ...interface{}) {
fmt.Println("Protected")
vargs := make([]reflect.Value, len(args))
for n, v := range args {
vargs[n] = reflect.ValueOf(v)
}
reflect.ValueOf(oldfunc).Call(vargs)
}
}
func main() {
a := func() {
fmt.Println("unprotected")
}
b := func(s string) {
fmt.Println(s)
}
c := protect(a)
d := protect(b)
c()
d("hello")
}
Ouput is
Protected
unprotected
Protected
hello
EDIT: To answer the update
Like I said above, types are pretty concrete in Go. The protect function returns a type func(...interface{}) which will never be assignable to func(int)int. I think you're probably either over-engineering your problem or misunderstanding it. However, here's a highly discouraged code snippet that would make it work.
First change protect to also return values:
func protect(oldfunc interface{}) (func (...interface{}) []interface{}) {
if reflect.TypeOf(oldfunc).Kind() != reflect.Func {
panic("protected item is not a function")
}
return func (args ...interface{}) []interface{} {
fmt.Println("Protected")
vargs := make([]reflect.Value, len(args))
for n, v := range args {
vargs[n] = reflect.ValueOf(v)
}
ret_vals := reflect.ValueOf(oldfunc).Call(vargs)
to_return := make([]interface{}, len(ret_vals))
for n, v := range ret_vals {
to_return[n] = v.Interface()
}
return to_return
}
}
Then make a convert function:
func convert(f func(...interface{}) (func(int) int) {
return func(x int) int {
r := f(x)
return r[0].(int)
}
}
Then your call would look like
take_func_int_int(convert(b))
But I promise this isn't what you actually want to do.
Step back and try to rework the problem. I've completely killed type-safety in these examples. What are you trying to accomplish?
package main
import "fmt"
// Here's a function that will take an arbitrary number
// of `int`s as arguments.
func sum(nums ...int) {
fmt.Print(nums, " ")
total := 0
for _, num := range nums {
total += num
}
fmt.Println(total)
}
func main() {
// Variadic functions can be called in the usual way
// with individual arguments.
sum(1, 2)
sum(1, 2, 3)
// If you already have multiple args in a slice,
// apply them to a variadic function using
// `func(slice...)` like this.
nums := []int{1, 2, 3, 4}
sum(nums...)
}

Resources