Golang supports two different ways to pass arguments to the function i.e. Pass by Value or Call by Value and Pass By Reference or Call By Reference. By default, Golang uses the call by value way to pass the arguments to the function.
A parameter is the variable listed inside the parentheses in the function definition.
Syntax. In the Go language, we declare a function by typing the func keyword. After typing in the func keyword, we have to write the name of our function. Following that, we list the parameters (if any are present), and then list the return type of the function being defined.
Command-line arguments are a way to provide the parameters or arguments to the main function of a program. Similarly, In Go, we use this technique to pass the arguments at the run time of a program. In Golang, we have a package called as os package that contains an array called as “Args”.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With