On Mon, 6 Jul 2015 01:31:34 -0700 (PDT)
pgb...@gmail.com wrote:
> I`m new in Golang and have a problem with calling a package
> function/s by string name from another package.
You can't. At least, you can't directly. That is, is your package "B"
imports package "A" which exports function "Foo", there's no way to
call B's "Foo" given a *string* constant/value containing that name.
There's several thing you can do about this:
1) Do not attempt to call anything by name.
You supposedly came from PHP or some other dynamic language
and are trying to apply your learned skills here.
This is understandable but usually wrong.
2) What you want to do is called mapping: you want to map a string
containing certain value to a function.
But Go already has maps for you.
Supposed that B.Foo has signature
func (i int) (string, error)
Then just go ahead and prepare a map:
var funcs = map[string](int) (string, error) {
"Foo": B.Foo,
}
and then you'll be able to call
funcs["Foo"](42)
(Playground link:
http://play.golang.org/p/NuleqshVdm).
[...]