How to call function from struct in slice?

236 views
Skip to first unread message

Jay

unread,
Jun 2, 2022, 12:17:04 PM6/2/22
to golang-nuts


In a code example, I’m confuse if it’s possible to call ModuleName() each package API after execute CompileModules()?


app.go


func CompileModules() []interface{

    return []interface{

        Example1.AddModule(),

        Example2.AddModule(),

        Example3.AddModule(),

    }

}




Init.go in Hello1 folder:

package Example1


type AddModule struct {}


func (p *AddModule) ModuleName() string {

    return “Hello 1”

}

Henry

unread,
Jun 4, 2022, 3:40:15 AM6/4/22
to golang-nuts
You need to cast the generic interface{} to its appropriate type. For instance:

```
modules := CompileModules()
for _, module := range modules {
      mod, ok := module.(*AddModule)
      if !ok {
            //error handling if module is not of type AddModule
      }
      fmt.Println(mod.ModuleName())
}
```

James Lei

unread,
Jun 4, 2022, 3:37:36 PM6/4/22
to golang-nuts
To add on, the return value of mod is "<nil>"

fmt.Println(mod)


James Lei

unread,
Jun 4, 2022, 3:37:36 PM6/4/22
to golang-nuts

I tried your solution and it still can't find .ModuleName(), 

modules := vendorpackages.CompileModules()
    for _, module := range modules {
        mod, ok := module.(*AddModule)
    if !ok {
        //error handling if module is not of type AddModule
    }
    fmt.Println(mod.ModuleName()) // No such method
}

Does it mean I'll need to declare method in AddModule struct?

type AddModule struct {
    ModuleName()
}



On Saturday, 4 June 2022 at 11:40:15 UTC+8 Henry wrote:

James Lei

unread,
Jun 4, 2022, 3:37:37 PM6/4/22
to golang-nuts
I have nearly solve the problem, except I have to use cast as mod.(*Example1.AddModule) which is wrong.

How do you cast as mod.(*AddModule)?





On Saturday, 4 June 2022 at 11:40:15 UTC+8 Henry wrote:

jake...@gmail.com

unread,
Jun 4, 2022, 4:04:54 PM6/4/22
to golang-nuts
I find your example code confusing, and it appears to have multiple syntax errors, which make it impossible to compile. If you could post a complete "working" example to the playground you would probably get a better response.

Little Ant

unread,
Jun 4, 2022, 6:56:43 PM6/4/22
to golang-nuts
Thank you Jake, 

I have not have a complete working solution, but I have included comments to understand the feature I'm trying to build to add or remove my custom modules similar to written CMS that add and remove modules/plugins. Unsure if I should go by "receiver" way or method way.

Henry

unread,
Jun 5, 2022, 4:20:00 AM6/5/22
to golang-nuts
I have no idea what problem you are facing. Your example is not clear enough to me. I think you may have several other problems in the mix as well (such as calling code from another package, etc.). Try reading the compiler error and see if it helps. 

I have included a more elaborate example. Hopefully, it helps point you toward the right direction. https://go.dev/play/p/HMnjBnl7eL5  

jake...@gmail.com

unread,
Jun 8, 2022, 4:09:43 PM6/8/22
to golang-nuts
Your playground example was still not runnable.
I have fixed it so it runs: https://go.dev/play/p/I57OftEerLY
Now that we have a working example, what precisely is the problem you are having?
Reply all
Reply to author
Forward
0 new messages