Home  >  Article  >  Backend Development  >  Pass method parameters to function

Pass method parameters to function

王林
王林forward
2024-02-14 08:21:081034browse

Pass method parameters to function

In PHP development, passing method parameters to functions is a common requirement. By passing parameters to functions, we can operate and process the parameters inside the function, thus achieving more flexible and reusable code. In this article, PHP editor Baicao will introduce how to correctly pass method parameters to functions, and share some practical tips and precautions to help developers better understand and apply this important programming concept. Whether you are a beginner or an experienced developer, this article will provide you with valuable reference and guidance.

Question content

I'm curious if this is possible in go. I have a type with multiple methods. Is it possible to have a function that takes a method parameter and then calls it for that type?

Here is a small example of what I want:

package main

import (
    "fmt"
)

type Foo int

func (f Foo) A() {
    fmt.Println("A")
}
func (f Foo) B() {
    fmt.Println("B")
}
func (f Foo) C() {
    fmt.Println("C")
}

func main() {
    var f Foo
    bar := func(foo func()) {
        f.foo()
    }
    bar(A)
    bar(B)
    bar(C)
}

go assumes that type foo has a method named foo() instead of replacing it with the passed in method name.

Solution

Yes, it is possible. You have 2 (3) choices:

Specification: Method expression

expressionfoo.a produces a function equivalent to a, but with an explicit receiver as its first argument; it has the signature func(f foo ).

var foo foo
bar := func(m func(f foo)) {
    m(foo)
}
bar(foo.a)
bar(foo.b)
bar(foo.c)

The receiver of this method is explicit. You simply pass the method name (and its type) to bar(), and when calling it, you must pass the actual receiver: m(f).

Output as expected (tried on go playground):

a
b
c

Specification: Method value

If f is a value of type foo, then the expression f.a generates a function value of type func(), which implicitly The receiver value is f.

var f foo
bar := func(m func()) {
    m()
}
bar(f.a)
bar(f.b)
bar(f.c)

Note that the method receiver here is implicit, it is saved with the function value passed to bar(), so it can be called without explicitly specifying it: m( ).

The output is the same (try it on go playground).

(For completeness: Reflection)

Lower than the previous solution (in terms of performance and "security"), but you can pass the method name as a string value and then use reflect Package to call the method by this name. It might look like this:

var f Foo
bar := func(name string) {
    reflect.ValueOf(f).MethodByName(name).Call(nil)
}
bar("A")
bar("B")
bar("C")

Try it on go playground.

The above is the detailed content of Pass method parameters to function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete