我从头到尾实现了一个Golang的依赖注入框架,并且集成了gin、xorm、redis、cron、消息中间件等功能;自己觉得还挺好用的,推荐给你!也欢迎一起维护!
github地址:https://github.com/gone-io/gone
文档地址:https://goner.fun/
如果可能,请帮忙在github上点个 ⭐️ ;万分感谢!!
在Gone中,我们做了一个有趣的定义:Goner所有属性都被注入后,我们称这个Goner被复活了(Revive)。如果你查看Gone的源代码,你会发现管理复活Goner状态的组件叫Heaven(天堂),这里灵感来源于各宗教神话中人死后会上天堂的传说。
为了执行Goner业务代码,我们可以在Goner上定义了方法 AfterRevive() gone.AfterReviveError
,这个方法会在Goner被 Revive 后得到执行,并且我们将拥有该方法的 Goner 称之为 Prophet(也就是 先知)。实际上,一般情况我们只需要定义少量的先知来引导代码执行就可以了。
下面是一个例子;代码可以在这里找到:
package main
import "github.com/gone-io/gone"
type Adder struct {
gone.Flag
}
func (a *Adder) Add(a1, a2 int) int {
return a1 + a2
}
type Computer struct {
gone.Flag
adder Adder `gone:"*"`
}
func (c *Computer) Compute() {
println("I want to compute!")
println("1000 add 2000 is", c.adder.Add(1000, 2000))
}
// AfterRevive 复活后执行的函数
func (c *Computer) AfterRevive() gone.AfterReviveError {
// boot
c.Compute()
return nil
}
func main() {
gone.Run(func(cemetery gone.Cemetery) error {
cemetery.Bury(&Computer{})
cemetery.Bury(&Adder{})
return nil
})
}
执行上面代码,将得到结果:
求赞助
如果觉得还可以,请帮忙在github上点个 ⭐️吧:
github地址:https://github.com/gone-io/gone
福利:🔥添加交流群,赠送 Golang 多套 学习资料,夯实基础👍🏻👍🏻
上一篇:Gone框架介绍1 - 在go语言上实现依赖注入,Gone是如何实现的?
下一篇:Gone框架介绍3 - 使用gone命令,自动生成Priest函数