Call method in other package by name?

user1493543

I'm trying to call a method in another package by the name of the method (using the reflect package) but I'm not sure exactly how to do it.

What I'm trying so far is,

reflect.ValueOf(controller).MethodByName(action_name).Call()

(where controller is the other package)

Any ideas?

nemo

You can't do this using pkg/reflect. For this to work, packages would need to be first class citizens, which they are not.

Your best bet is to store the functions you want to access in a map[string]interface{} and look up the function in the map:

func Foo() { println("foo?") }

m := map[string]interface{}{
    "foo": Foo
}

f := m["foo"].(func())
f()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call function in other method

From Dev

c# Call class Member (property,method,etc) by dymanic name from Other class and method

From Dev

Dynamically call method name?

From Dev

Call other templates with dynamic name

From Dev

PHP. call static method of class which class name saved in static variable of other class?

From Dev

Call method inside of other model

From Dev

Cannot call method in other class

From Dev

Call a method on a dynamic object by name

From Dev

Call to undefined method Route::name()

From Dev

Calling of one method in matlab package inside other method of same package

From Dev

CALL METHOD with dynamic method name, RuntimeException

From Dev

Method call itself without method name?

From Dev

Call a method using a variable containing the method name

From Dev

Method call itself without method name?

From Dev

Getting Full Package Name for RemovePackageAsync() call

From Dev

Django redirect call appends package name to url

From Dev

Public method invisible to other class in same package

From Dev

Call java main method during maven package

From Dev

How to assign a __call__ method to a module/package?

From Dev

package method with same name as perl builtin

From Dev

how to call other method on karma unittest?

From Dev

Call a method any time other methods are called

From Dev

Call long running method and continue with other tasks

From Dev

How to call other class method with out parameter

From Dev

Call method, defined in Application controller, in other controllers

From Dev

Android - Call to a method with AsyncTask disrupts other methods

From Dev

Make instance of "object" to other class and call method

From Dev

Click EventHandler : Method Name Expected and other Errors

From Dev

Call method from class of unknown name

Related Related

  1. 1

    Call function in other method

  2. 2

    c# Call class Member (property,method,etc) by dymanic name from Other class and method

  3. 3

    Dynamically call method name?

  4. 4

    Call other templates with dynamic name

  5. 5

    PHP. call static method of class which class name saved in static variable of other class?

  6. 6

    Call method inside of other model

  7. 7

    Cannot call method in other class

  8. 8

    Call a method on a dynamic object by name

  9. 9

    Call to undefined method Route::name()

  10. 10

    Calling of one method in matlab package inside other method of same package

  11. 11

    CALL METHOD with dynamic method name, RuntimeException

  12. 12

    Method call itself without method name?

  13. 13

    Call a method using a variable containing the method name

  14. 14

    Method call itself without method name?

  15. 15

    Getting Full Package Name for RemovePackageAsync() call

  16. 16

    Django redirect call appends package name to url

  17. 17

    Public method invisible to other class in same package

  18. 18

    Call java main method during maven package

  19. 19

    How to assign a __call__ method to a module/package?

  20. 20

    package method with same name as perl builtin

  21. 21

    how to call other method on karma unittest?

  22. 22

    Call a method any time other methods are called

  23. 23

    Call long running method and continue with other tasks

  24. 24

    How to call other class method with out parameter

  25. 25

    Call method, defined in Application controller, in other controllers

  26. 26

    Android - Call to a method with AsyncTask disrupts other methods

  27. 27

    Make instance of "object" to other class and call method

  28. 28

    Click EventHandler : Method Name Expected and other Errors

  29. 29

    Call method from class of unknown name

HotTag

Archive