Go - how to organize code for dynamic packages -


i have small web application written in go. created base larger system , extendable components can added/removed without needing base modified in way.

the structure currently:

       app          modules              core                  ... core files here ...        app.go    main.go 

app.go contain routing method should take web request , based on request path know module responsible handling request. each module/component having on controller.

each component have own package name think going impossible since go forces explicit import.

for example may add new module/component called blog such as:

       app          modules              core                  ... core files here ...                  controller.go              blog                  ... blog files here ...                  controller.go        app.go    main.go 

there several ways achieve goal. since go not support dynamically loaded libraries @ moment, need recompile application whenever add/remove components. simplest way therefore yourapp/core package following:

  • an application type servehttp method acts main application
  • a component interface components have implement. might want include baseurl() string , servehttp method there.
  • a register method application type, can used add new components app.

your components can implemented in separate packages (e.g. yourapp/blog) , depend on yourapp/core package.

the thing still needs "user-editable" main.go file, might this:

func main() {     app := core.newapplication()     app.register(blog.blog{         title: "my personal blog",     })     app.register(...)     app.run() } 

another approach might define rpc interface components (which might include functions registercomponent, unregistercomponent , getglobalconfig).

then, can run components in separate processes has advantage can start/stop/reload components dynamically , can not break main app. take @ net/rpc package , maybe httputil.newsinglehostreverseproxy if want use approach instead.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -