{"id":43786051,"url":"https://github.com/coditory/go-di","last_synced_at":"2026-02-05T19:03:59.566Z","repository":{"id":92840913,"uuid":"605544871","full_name":"coditory/go-di","owner":"coditory","description":"Go Dependency Injection library","archived":false,"fork":false,"pushed_at":"2025-12-15T10:05:12.000Z","size":107,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-12-18T10:04:23.415Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coditory.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["coditory"]}},"created_at":"2023-02-23T11:38:53.000Z","updated_at":"2025-12-15T10:05:16.000Z","dependencies_parsed_at":"2024-04-29T11:26:37.943Z","dependency_job_id":"f76d0169-556a-482f-a2c7-7cc7af5d5b39","html_url":"https://github.com/coditory/go-di","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":"0.040000000000000036","last_synced_commit":"a2f35d12bf802d04ea2b2dd7b9e3a232824dc002"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/coditory/go-di","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditory%2Fgo-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditory%2Fgo-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditory%2Fgo-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditory%2Fgo-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coditory","download_url":"https://codeload.github.com/coditory/go-di/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coditory%2Fgo-di/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29130113,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T18:55:47.139Z","status":"ssl_error","status_checked_at":"2026-02-05T18:55:04.010Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":[],"created_at":"2026-02-05T19:03:59.505Z","updated_at":"2026-02-05T19:03:59.559Z","avatar_url":"https://github.com/coditory.png","language":"Go","readme":"# Coditory - Go Dependency Injection\n[![GitHub release](https://img.shields.io/github/v/release/coditory/go-di.svg)](https://github.com/coditory/go-di/releases)\n[![Go Reference](https://pkg.go.dev/badge/github.com/coditory/go-di.svg)](https://pkg.go.dev/github.com/coditory/go-di)\n[![Go Report Card](https://goreportcard.com/badge/github.com/coditory/go-di)](https://goreportcard.com/report/github.com/coditory/go-di)\n[![Build Status](https://github.com/coditory/go-di/workflows/Build/badge.svg?branch=main)](https://github.com/coditory/go-di/actions?query=workflow%3ABuild+branch%3Amain)\n[![Coverage](https://codecov.io/gh/coditory/go-di/branch/main/graph/badge.svg?token=EPRs5LiPje)](https://codecov.io/gh/coditory/go-di)\n\n**🚧 This library as under heavy development until release of version `1.x.x` 🚧**\n\n\u003e Dependency injection for Go projects, targeted for web applications that create DI context during the startup and operates on collections of dependencies.\n\n- Register dependencies by name and type\n- Register multiple dependencies per type\n- Conditional dependency registration\n- Simple dependency retrieval - no manual casting or additional callbacks\n- Simple setup - no generators\n- Detection of slow dependency creation (TODO)\n- Initialization and finalization mechanisms (TODO)\n\n# Getting started\n\n## Installation\nGet the dependency with:\n```sh\ngo get github.com/coditory/go-di\n```\n\nand import it in the project:\n```go\nimport \"github.com/coditory/go-di\"\n```\n\nThe exported package is `di`, basic usage:\n```go\nimport \"github.com/coditory/go-di\"\n\nfunc main() {\n  ctxb := di.NewContextBuilder()\n  // Add dependencies\n  ctxb.Add(\u0026foo)\n  // Build the di context\n  ctx := ctxb.Build()\n  // Retrieve dependencies from the context\n}\n```\n\n## Full example\n\n```go\npackage main\n\nimport (\n  \"fmt\"\n  \"github.com/coditory/go-di\"\n)\n\ntype Baz interface { Id() string }\ntype Foo struct { id string }\nfunc (f *Foo) Id() string { return f.id }\ntype Bar struct { id string }\nfunc (b *Bar) Id() string { return b.id }\n\nfunc main() {\n  foo := Foo{id: \"foo1\"}\n  foo2 := Foo{id: \"foo2\"}\n  bar := Bar{id: \"baz\"}\n\n  ctxb := di.NewContextBuilder()\n  ctxb.AddAs(new(Baz), \u0026foo)\n  ctxb.AddAs(new(Baz), \u0026foo2)\n  ctxb.AddAs(new(Baz), \u0026bar)\n  ctx := ctxb.Build()\n\n  fmt.Printf(\"first implementation: %+v\\n\", di.Get[Baz](ctx))\n  for i, baz := range di.GetAll[Baz](ctx) {\n    fmt.Printf(\"%d: %+v\\n\", i, baz)\n  }\n}\n// output:\n// first implementation: \u0026{id:foo1}\n// 0: \u0026{id:foo1}\n// 1: \u0026{id:foo2}\n// 2: \u0026{id:baz}\n```\n\n# Usage\n\n## Dependencies by type\n\nAdd dependency reference and retrieve by the reference type:\n```go\nctxb := di.NewContextBuilder()\nctxb.Add(\u0026foo)\nctxb.Add(\u0026foo2)\nctx := ctxb.Build()\nsuite.Equal(\u0026foo, di.Get[*Foo](ctx))\nsuite.Equal([]*Foo{\u0026foo, \u0026foo2}, di.GetAll[*Foo](ctx))\n```\n\nAdd dependency (by value) and retrieve by the type:\n```go\nctxb := di.NewContextBuilder()\nctxb.Add(foo)\nctxb.Add(foo2)\nctx := ctxb.Build()\nsuite.Equal(foo, di.Get[Foo](ctx))\nfoos := di.GetAll[Foo](ctx)\nsuite.Equal(2, len(foos))\nsuite.Equal(\"foo\", foos[0].Id())\nsuite.Equal(\"foo2\", foos[1].Id())\n```\n\n## Dependencies by interface\n\nTo retrieve a dependency by the interface it must be registered explicitly by the interface type:\n```go\nctxb := di.NewContextBuilder()\n// ctx.Add(\u0026foo) \u003c- will not work!\nctxb.AddAs(new(Baz), \u0026foo)\nctxb.AddAs(new(Baz), \u0026foo2)\nctx := ctxb.Build()\nsuite.Equal(\u0026foo, di.Get[Baz](ctx))\nsuite.Equal([]Baz{\u0026foo, \u0026foo2}, di.GetAll[Baz](ctx))\n```\n\nSingle dependency can be registered multiple times:\n```go\nctxb := di.NewContextBuilder()\nctxb.Add(\u0026foo)\nctxb.AddAs(new(Baz), \u0026foo)\nctxb.Add(\u0026foo2)\nctxb.AddAs(new(Baz), \u0026foo2)\nctx := ctxb.Build()\nsuite.Equal(\u0026foo, di.Get[Baz](ctx))\nsuite.Equal([]Baz{\u0026foo, \u0026foo2}, di.GetAll[Baz](ctx))\nsuite.Equal(\u0026foo, di.Get[*Foo](ctx))\nsuite.Equal([]*Foo{\u0026foo, \u0026foo2}, di.GetAll[*Foo](ctx))\n```\n\n## Named dependencies\n\nTo differentiate dependencies of the same type you can name them.\nThere can be only one dependency per name.\n\n```go\nctxb := di.NewContextBuilder()\nctxb.AddAs(new(Baz), \u0026foo)\nctxb.AddNamedAs(\"special-foo\", new(Baz), \u0026foo2)\n// ctxb.AddNamedAs(\"special-foo\", new(Baz), \u0026foo3) \u003c- panics\nctx := ctxb.Build()\nsuite.Equal(\u0026foo, di.Get[Baz](ctx))\nsuite.Equal(\u0026foo2, di.GetNamed[Baz](ctx, \"special-foo\"))\nsuite.Equal([]Baz{\u0026foo, \u0026foo2}, di.GetAll[Baz](ctx))\n```\n\n## Lazy dependencies\n\nLazy dependencies are created when retrieved:\n\n```go\ntype Boo struct {\n  foo *Foo\n  bar *Bar\n}\nctxb := di.NewContextBuilder()\nctxb.Provide(func() *Foo {\n  return \u0026foo\n})\nctxb.Provide(func() *Bar {\n  return \u0026bar\n})\nctxb.Provide(func(foo *Foo, bar *Bar) *Boo {\n  return \u0026Boo{foo: foo, bar: bar}\n})\nctx := ctxb.Build()\nboo := di.Get[*Boo](ctx)\nsuite.Equal(\u0026foo, boo.foo)\nsuite.Equal(\u0026bar, boo.bar)\n```\n\nIf a dependency requires a long list of other dependencies then inject `*Context`:\n```go\nctxb.Provide(func(ctx *di.Context) *Boo {\n  return \u0026Boo{foo: di.Get[*Foo](ctx), bar: di.Get[*Bar](ctx)}\n})\n```\n\nThere are the same variations for `ctxb.Add*` and `ctxb.Provide*` functions:\n```go\nctxb.Provide(createFoo)\nctxb.ProvideAs(new(Baz), createFoo)\nctxb.ProvideNamed(\"special-foo\", createFoo)\nctxb.ProvideNamedAs(\"special-foo\", new(Baz), createFoo)\n```\n\nWhen dependency creator is a separate (non-inlined) function, then creation happens only once:\n```go\ncreations := 0\ncreateFoo := func() *Foo {\n  creations++\n  return \u0026foo\n}\nctxb := di.NewContextBuilder()\nctxb.Provide(createFoo)\nctxb.ProvideAs(new(Baz), createFoo)\nctx := ctxb.Build()\ndi.Get[Baz](ctx)\ndi.Get[*Foo](ctx)\nsuite.Equal(1, creations)\n```\n","funding_links":["https://github.com/sponsors/coditory"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoditory%2Fgo-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoditory%2Fgo-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoditory%2Fgo-di/lists"}