问题描述
我有一个用Go编写的小型Web应用程序。它被创建为一个更大的系统的基础,我希望它可以扩展,可以添加/删除组件,而无需以任何方式修改该基础。
目前的结构是:
App
模块
核心
...核心文件在这里...
app.go
main.go
app.go会包含一个路由方法,它应该接受一个web请求,并根据请求路径知道哪个模块负责处理请求。每个模块/组件都有其控制器。
每个组件都有它自己的包名,所以我认为这是不可能的,因为go强制显式导入。例如,我可以添加一个名为say blog的新模块/组件,例如:
App
模块
核心
...核心文件在这里...
controller.go
博客
...博客文件在这里。 ..
controller.go
app.go
main.go
有几种方法可以实现您的目标。由于Go目前不支持动态加载的库,因此无论何时添加/删除任何组件,都需要重新编译应用程序。因此,最简单的方法是使用 yourapp / core 软件包:
然后您的组件可以在单独的包中实现(例如 yourapp / blog )和他们可能会依赖于你的 yourapp / core 包。
唯一仍然需要可编辑是 main.go 文件,可能如下所示:
func main(){
app:= core.NewApplication()
app.Register(blog.Blog {
标题:我的个人博客,
})
app.Register(...)
app.Run()
}
另一种方法可能是为你的组件定义一个RPC接口(它可能包含像 RegisterComponent , UnregisterComponent 和 GetGlobalConfig )。然后,你可以在不同的进程中运行这些组件,这样做的好处是你可以动态地启动/停止/重新加载这些组件,并且它们不会破坏你的主应用程序。查看软件包,甚至可以,如果您想使用此方法。
I have a small web application written in Go. It is created a base for a larger system and I would like it to be extendable where components can be added/removed without needing this base to be modified in any way.
The structure is currently:
App Modules Core ... Core Files Here ... app.go main.go
app.go will contain a routing method which should take a web request and based on the request path know which module is responsible for handling the request. Each module/component having its on controller.
Each component will have its own package name so i think this is going to be impossible since go forces an explicit import.
For example i may add a new module/component called say blog such as:
App Modules Core ... Core Files Here ... controller.go Blog ... Blog Files Here ... controller.go app.go main.go
There are several ways to achieve your goal. Since Go does not support dynamically loaded libraries at the moment, you need to probably recompile your application whenever you add/remove any components. The simplest way therefore would be a yourapp/core package with the following:
- an Application type with an ServeHTTP method that acts as your main application
- a Component interface that all your components have to implement. Your might want include a BaseUrl() string and a ServeHTTP method there.
- a Register method to your Application type, that can be used to add new components to your app.
Your components can then be implemented in separate packages (e.g. yourapp/blog) and they will probably depend on your yourapp/core package.
The only thing that still needs to be "user-editable" is the main.go file, which might look like this:
func main() { app := core.NewApplication() app.Register(blog.Blog{ Title: "My Personal Blog", }) app.Register(...) app.Run() }
Another approach might be to define an RPC interface for your components (which might include functions like RegisterComponent, UnregisterComponent and a GetGlobalConfig).
Then, you can run those components in separate processes which has the advantage that you can start/stop/reload those components dynamically and they can not break your main app. Take a look at the net/rpc package and maybe even httputil.NewSingleHostReverseProxy if you want to use this approach instead.
这篇关于去 - 如何组织动态包的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!