How to dynamically add imports

huangapple go评论115阅读模式
英文:

How to dynamically add imports

问题

我想动态创建一个使用自定义插件/中间件的HTTP路由器,目前我正在使用一个config.yml文件作为示例:

plugins:
  - waf
  - jwt 
  - cors

解析yml文件后,我会像这样创建路由:

route := violetear.New()
chain := middleware.New() 
for _, plugin := range Plugins {
    chain := chain.Append(plugin)       
}
router.Handle("/test", chain.Then(myHandler))
log.Fatal(http.ListenAndServe(":8080", router))

为了使其工作,我需要在导入部分包含所有插件,类似于:

import (
   "net/http"

   "github.com/nbari/violetear"
   "github.com/nbari/violetear/middleware"
   
   // 如何处理这个
   "github.com/example/waf"
   "github.com/example/jwt"
   "github.com/example/cors"
)

我需要更改当前的配置格式,使其更有用/通用,可能是这样的:

plugins:
  - [github.com/foo, foo]
  - [github.com/bar, bar]

但除此之外,有什么最好的方法来“动态”创建导入或生成稍后可以编译的代码呢?

有什么想法吗?

英文:

I want to dynamically create an HTTP router using custom plugins/middleware, currently, I am using a config.yml for example:

plugins:
  - waf
  - jwt 
  - cors

After parsing the yml file I create the routes like this:

route := violetear.New()
chain := middleware.New() 
for _, plugin := range Plugins {
    chain := chain.Append(plugin)       
}
router.Handle("/test", chain.Then(myHandler))
log.Fatal(http.ListenAndServe(":8080", router))

For this to work, I would have to include all the plugins in the import section, something like:

import (
   "net/http"

   "github.com/nbari/violetear"
   "github.com/nbari/violetear/midleware"
   
   // How to deal with this
   "github.com/example/waf"
   "github.com/example/jwt"
   "github.com/example/cors"
)

I would need to change the current config format to be something more useful/generic probably something like:

plugins:
  - [github.com/foo, foo]
  - [github.com/bar, bar]

But besides that what could be the best approach to "dynamically" create the imports or to generate the code the one later could be compiled?

Any ideas?

答案1

得分: 1

Go是一种静态链接语言。这意味着如果一个包在编译时没有被引用(从你的.go源文件中),那个包将不会被链接/编译到可执行二进制文件中,这意味着它在运行时将不可用。

所以最简单的方法就是使用导入。

如果你想要真正的动态配置,Go 1.8引入的插件可能是一个选择,但是使用插件会使事情变得复杂,我只会把它作为最后的选择。还要注意,插件目前只在Linux上工作。

相关问题:

https://stackoverflow.com/questions/30097451/dynamic-loading-in-golang/30102141#30102141

https://stackoverflow.com/questions/43181410/go-plugin-variable-initialization/43181795#43181795

https://stackoverflow.com/questions/42388090/go-1-8-plugin-use-custom-interface/42389345#42389345

英文:

Go is a statically linked language. This means if a package is not referenced at compile time (from your .go source files), that package will not be linked / compiled into the executable binary, which implies it will not be availabe at runtime.

So the easiest is to just use imports.

If you want truly dynamic configuration, plugins introduced in Go 1.8 may be an option for you, but using plugins complicates things and I would only use that as a last resort. Also note that plugins currently only work on linux.

Related questions:

https://stackoverflow.com/questions/30097451/dynamic-loading-in-golang/30102141#30102141

https://stackoverflow.com/questions/43181410/go-plugin-variable-initialization/43181795#43181795

https://stackoverflow.com/questions/42388090/go-1-8-plugin-use-custom-interface/42389345#42389345

huangapple
  • 本文由 发表于 2017年5月29日 21:40:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/44243967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定