推荐依赖注入库: go-container

tuzhiya · 2024-08-27 12:54:09 · 761 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2024-08-27 12:54:09 的主题,其中的信息可能已经有所发展或是发生改变。

go-container 是使用go的反射使用的轻量级的依赖注入库,使用简单

下载:

go get -u github.com/deatil/go-container

示例:

package main

import (
    "fmt"
    "github.com/deatil/go-container/container"
)

type testBind struct {}

func (t *testBind) Data() string {
    return "testBind data"
}

// 更多示例可以查看测试文件
func main() {
    // Bind func
    di := container.DI()
    di.Bind("testBind", func() *testBind {
        return &testBind{}
    })
    tb2 := di.Get("testBind").(*testBind)

    fmt.Printf("output: %s", tb2.Data())
    // output: testBind data
}

项目地址: https://github.com/deatil/go-container


有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

761 次点击  ∙  1 赞  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传