It there a better way?<br>
Add new MVC routines in a logical and easier way.<br/>
Must support adding new packages easily.<br/>
Every package must have one or more functions.<br/>
The goal is to add functions to a command map that is used to route network
calls.
GO can not dynamically link new modules. gowss (github/greenpdx/gowss) is a
MVC system. It parses incoming commands and calls the correct function. I
wanted a way to add new commands easily, this means at startup all the
commands need to be known. I also wanted to be able to add new packages to
the main program. <br/>
I first create a common interface to the commands.<br/>
passed in is a connection arg and a map arg, it returns a map<br>
type Command func( conn *Connection, jin map[string]interface{})
map[string]interface{}<br/>
Now I have a module structure:<br/>
type Module struct {<br/>
Name string<br/>
Procs map[string]Command<br>
Data interface{}<br>
}<br/>
it has a name, a map of commands, and user defined data.
The init function is used to setup the module
var Module gows.Module<br/>
func init() {<br>
// create a module structure
mod := gows.Module{<br/>
Name: "mod1",<br/>
Data: nil,<br/>
Procs: make(map[string]gows.Command),<br/>
}<br/>
// Fill in the commands map
mod.Procs["func1"] = func1<br/>
mod.Procs["func2"] = func2<br/>
<br/>
Module = mod<br/>
fmt.Println("mod1 init",mod.Name, mod.Procs)<br/>
}<br/>
In the main program I add each module to the import.<br/>
import {<br/>
......<br/>
"./mod1"<br/>
}<br/>
then during program initialization I add the modules functions to the
command list.<br>
func AddMods() {<br/>
gows.Modules["mod1"] = &mod1.Module<br/>
<br/>
mod1.Module.Add(&mod1.Module)<br/>
}<br/>
Now when a command comes in I use the function name to call to correct
function from the command list.
Is there a better or easier way to do this?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.