{"id":17081962,"url":"https://github.com/studyzy/iocgo","last_synced_at":"2026-03-10T14:34:01.425Z","repository":{"id":52997081,"uuid":"415223687","full_name":"studyzy/iocgo","owner":"studyzy","description":"A lightweight  Inversion of Control (IoC) (Dependency Injection) container for Golang","archived":false,"fork":false,"pushed_at":"2022-01-02T16:56:16.000Z","size":54,"stargazers_count":38,"open_issues_count":0,"forks_count":9,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-19T22:14:41.193Z","etag":null,"topics":["dependency-injection","go","golang","inversion-of-control","ioc","ioc-container"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/studyzy.png","metadata":{"files":{"readme":"README-zh_CN.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-09T06:30:56.000Z","updated_at":"2025-01-26T08:15:59.000Z","dependencies_parsed_at":"2022-09-08T00:51:18.401Z","dependency_job_id":null,"html_url":"https://github.com/studyzy/iocgo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/studyzy/iocgo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studyzy%2Fiocgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studyzy%2Fiocgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studyzy%2Fiocgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studyzy%2Fiocgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/studyzy","download_url":"https://codeload.github.com/studyzy/iocgo/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studyzy%2Fiocgo/sbom","scorecard":{"id":856250,"data":{"date":"2025-08-11","repo":{"name":"github.com/studyzy/iocgo","commit":"54d474ad4d2a40b76d61cde197caa17a60856e1e"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.4,"checks":[{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/14 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/go.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/studyzy/iocgo/go.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/go.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/studyzy/iocgo/go.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/go.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/studyzy/iocgo/go.yml/main?enable=pin","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   1 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 5 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-23T23:57:42.710Z","repository_id":52997081,"created_at":"2025-08-23T23:57:42.710Z","updated_at":"2025-08-23T23:57:42.710Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30337297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T12:41:07.687Z","status":"ssl_error","status_checked_at":"2026-03-10T12:41:06.728Z","response_time":106,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","go","golang","inversion-of-control","ioc","ioc-container"],"created_at":"2024-10-14T12:58:11.949Z","updated_at":"2026-03-10T14:34:01.408Z","avatar_url":"https://github.com/studyzy.png","language":"Go","readme":"# 1. iocgo简介\n习惯于Java或者C#开发的人应该对控制反转与依赖注入应该再熟悉不过了。在Java平台有鼎鼎大名的Spring框架，在C#平台有Autofac,Unity,Windsor等，我当年C#开发时用的最多的就是Windsor。使用IoC容器是面向对象开发中非常方便的解耦模块之间的依赖的方法。各个模块之间不依赖于实现，而是依赖于接口，然后在构造函数或者属性或者方法中注入特定的实现，方便了各个模块的拆分以及模块的独立单元测试。\n在[长安链]的设计中，各个模块可以灵活组装，模块之间的依赖基于protocol中定义的接口，每个接口有一个或者多个官方实现，当然第三方也可以提供该接口更多的实现。为了实现更灵活的组装各个模块，管理各个模块的依赖关系，于是我写了iocgo这个轻量级的golang版Ioc容器。\n\n[English]((https://github.com/studyzy/iocgo/blob/main/README.md)) | 中文\n\n# 2. iocgo如何使用\n## 2.1 iocgo包的安装\n现在go官方版本已经出到1.17了，当然我在代码中其实也没有用什么新版本的新特性，于是就用1.15版本或者之后的Go版本即可。要使用iocgo包，直接通过go get添加到项目中：\n\n`go get github.com/studyzy/iocgo`\n\n## 2.2 使用示例与说明\n### 2.2.1 最简单的例子：\n```go\ntype Fooer interface {\n\tFoo(int)\n}\ntype Foo struct {\n}\n\nfunc (Foo)Foo(i int)  {\n\tfmt.Println(\"foo:\",i)\n}\ntype Barer interface {\n\tBar(string)\n}\ntype Bar struct {\n\n}\nfunc (Bar) Bar(s string){\n\tfmt.Println(\"bar:\",s)\n}\ntype Foobarer interface {\n\tSay(int,string)\n}\ntype Foobar struct {\n\tfoo Fooer\n\tbar Barer\n}\nfunc NewFoobar(f Fooer,b Barer) Foobarer{\n\treturn \u0026Foobar{\n\t\tfoo: f,\n\t\tbar: b,\n\t}\n}\nfunc (f Foobar)Say(i int ,s string)  {\n\tf.foo.Foo(i)\n\tf.bar.Bar(s)\n}\nfunc TestContainer_SimpleRegister(t *testing.T) {\n\tcontainer := NewContainer()\n\tcontainer.Register(NewFoobar)\n\tcontainer.Register(func() Fooer { return \u0026Foo{} })\n\tcontainer.Register(func() Barer { return \u0026Bar{} })\n\tvar fb Foobarer\n\tcontainer.Resolve(\u0026fb)\n\tfb.Say(123,\"Hello World\")\n}\n```\n这里我使用NewContainer()创建了一个新的容器，然后在容器中调用Register方法注册了3个接口和对应的构造函数，分别是：\n1. Foobarer接口对应NewFoobar(f Fooer,b Barer)构造函数\n2. Fooer接口对应构造\u0026Foo{}的匿名函数。\n3. Barer接口对应构造\u0026Bar{}的匿名函数。\n\n接下来调用Resolve函数，并传入var fb Foobarer 这个接口变量的指针，iocgo就会自动去构建Foobarer对应的实例，并最终将实例赋值到fb这个变量上，于是最后我们就可以正常调用fb.Say实例方法了。\n\n### 2.22. Register 的选项\niocgo的注册interface到对象的函数定义如下：\n\n`func Register(constructor interface{}, options ...Option) error`\n\niocgo为Register函数提供了以下参数选项可根据实际情况选择性使用:\n* Name 为某个interface-\u003e对象的映射命名\n* Optional 表名这个构造函数中哪些注入的interface参数是可选的，如果是可选，那么就算找不到interface对应的实例也不会报错。\n* Interface 显式声明这个构造函数返回的实例是映射到哪个interface。\n* Lifestyle(isTransient) 声明这个构造函数在构造实例后是构造的临时实例还是单例实例，如果是临时实例，那么下次再获取该interface对应的实例时需要再次调用构造函数，如果是单例，那么就缓存实例到容器中，下次再想获得interface对应的实例时直接使用缓存中的，不需要再次构造。\n* DependsOn 这个主要是指定构造函数中的某个参数在通过容器获得对应的实例时，应该通过哪个Name去获得对应的实例。\n* Parameters 这个主要用于指定构造函数中的某些非容器托管的参数，比如某构造函数中有int，string等参数，而这些参数的实例是不需要通过ioc容器进行映射托管的，那么就在这里直接指定。\n* Default 这个主要用于设置一个interface对应的默认的实例，也就是如果没有指定Name的情况下，应该找哪个实例。\n  关于每一个参数该如何使用，我都写了UT样例，具体参考：\n  [container_test.go](https://github.com/studyzy/iocgo/blob/main/container_test.go)\n\n### 2.2.3. 注册实例\n如果我们已经有了某个对象的实例，那么可以将该实例和其想映射的interface直接注册到ioc容器中，方便其他依赖的对象获取，RegisterInstance函数定义如下:\n\n`RegisterInstance(interfacePtr interface{}, instance interface{}, options ...Option) error `\n使用上也很简单，直接将实例对应的interface的指针作为参数1，实例本身作为参数2，传入RegisterInstance即可：\n```go\nb := \u0026Bar{}\nvar bar Barer //interface\ncontainer.RegisterInstance(\u0026bar, b) // register interface -\u003e instance\n```\n\n### 2.2.4. 获得实例\n相关映射我们通过Register函数和RegisterInstance函数已经注册到容器中，接下来就需要从容器获得指定的实例了。获得实例需要调用函数：\n\n`func Resolve(abstraction interface{}, options ...ResolveOption) error`\n这里第一个参数abstraction是我们想要获取的某个interface的指针，第二个参数是可选参数，目前提供的选项有：\n* ResolveName 指定使用哪个name的interface和实例的映射，如果不指定，那么就是默认映射。\n* Arguments 指定在调用对应的构造函数获得实例时，传递的参数，比如int，string等类型的不在ioc容器中托管的参数，可以在这里指定。如果构造函数本身需要这些参数，而且在前面Register的时候已经通过Parameters选项进行了指定，那么这里新的指定会覆盖原有Register的指定。\n```go\nvar fb Foobarer\nerr:=container.Resolve(\u0026fb)\n```\n另外如果我们的构造函数return的值中支持error，而且实际构造的时候确实返回了error，那么Resolve函数也会返回对应的这个err。\n**特别注意：Resolve的第一个参数是申明的某个interface的指针，一定要是指针，不能直接传interface**\n\n### 2.2.5. 结构体参数和字段填充\n有些时候构造函数的入参非常多，于是我们可以申明一个结构体，把所有入参都放入这个结构体中，这样构造函数就只需要一个参数了。iocgo也支持自动填充这个结构体中interface对应的实例，从而构造新的对象。另外iocgo也提供了Fill方法，可以直接填充某个结构体，比如：\n```go\ntype FoobarInput struct {\n\tfoo Fooer\n\tbar Barer\n\tmsg string\n}\ninput := FoobarInput{\n\t\tmsg: \"studyzy\",\n\t}\n\tcontainer.Register(func() Fooer { return \u0026Foo{} })\n\tcontainer.Register(func() Barer { return \u0026Bar{} })\n\terr := container.Fill(\u0026input)\n```\n结构体中的字段还支持tag，目前提供的tag有两种：\n\n* name //指定这个字段在获得对应的实例时使用的name\n* optional //指定这个字段是否是可选的，如果是，那么就算获得不到对应的实例，也不会报错。\n  示例example:\n```\ntype FoobarInputWithTag struct {\n\tfoo Fooer `optional:\"true\"`\n\tbar Barer `name:\"baz\"`\n\tmsg string\n}\n```\n\n### 2.2.6. 函数调用\n除了构造函数注入之外，iocgo也支持函数注入，我们申明一个函数，这个函数的参数中有些参数是interface，那么通过调用iocgo中的Call方法，可以为这个函数注入对应的实例作为参数，并最终完成函数的调用。\n示例 example:\n```\nfunc SayHi1(f Fooer, b Barer) {\n\tf.Foo(1234)\n\tb.Bar(\"hi\")\n}\nRegister(func() Fooer { return \u0026Foo{} })\nRegister(func() Barer { return \u0026Bar{} })\nCall(SayHi1)\n```\nCall函数也是支持选项的，目前提供了2个选项:\n* CallArguments 指定函数中某个参数的值\n* CallDependsOn 指定函数中某个参数在通过ioc容器获得实例时使用哪个name来获得实例。\n  最后函数调用完成，如果函数本身有多个返回值，有error返回，那么Call函数也会返回对应的结果。\n\n\n## 2.3 参考:\n在写这个iocgo的代码时，主要参考了以下两个Ioc相关的项目：\n* https://github.com/golobby/container\n* https://github.com/castleproject/Windsor\n\n# 3. 总结\niocgo是一个纯Golang语言开发的用于管理依赖注入的IoC容器，使用这个容器可以很好的实现go语言下的面向对象开发，模块解耦。现已经开源，欢迎大家使用，开源地址：https://github.com/studyzy/iocgo","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstudyzy%2Fiocgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstudyzy%2Fiocgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstudyzy%2Fiocgo/lists"}