{"id":18717595,"url":"https://github.com/maksimzayats/godi","last_synced_at":"2025-07-11T12:09:48.010Z","repository":{"id":57694652,"uuid":"486937651","full_name":"MaksimZayats/godi","owner":"MaksimZayats","description":"🚀 GoDI: Generic based DI in Go","archived":false,"fork":false,"pushed_at":"2022-05-04T15:42:47.000Z","size":34,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-12T13:51:53.481Z","etag":null,"topics":["dependency-injection","di","go","go-dependency-injection","go-di","go-generics","go-library","injection-dependency"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MaksimZayats.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-04-29T10:55:08.000Z","updated_at":"2024-05-21T15:38:46.000Z","dependencies_parsed_at":"2022-09-26T21:20:40.623Z","dependency_job_id":null,"html_url":"https://github.com/MaksimZayats/godi","commit_stats":null,"previous_names":["maximzayats/go-typed-di","maksimzayats/godi","maximzayats/godi"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/MaksimZayats/godi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaksimZayats%2Fgodi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaksimZayats%2Fgodi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaksimZayats%2Fgodi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaksimZayats%2Fgodi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MaksimZayats","download_url":"https://codeload.github.com/MaksimZayats/godi/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MaksimZayats%2Fgodi/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264803974,"owners_count":23666514,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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","di","go","go-dependency-injection","go-di","go-generics","go-library","injection-dependency"],"created_at":"2024-11-07T13:16:52.782Z","updated_at":"2025-07-11T12:09:47.978Z","avatar_url":"https://github.com/MaksimZayats.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🚀 GoDI: Generic based DI in Go\n\n## Installation\n\n`DI`:\n\n* ```shell\n  go get -u github.com/MaximZayats/godi/\n  ```\n* ```go\n  import \"github.com/MaximZayats/godi/di\"\n  ```\n\n`CMD`:\n\n* ```shell\n  go get -u github.com/MaximZayats/godi/cmd/godi\n  ```\n* ```shell\n  go run github.com/MaximZayats/godi/cmd/godi init ./storage/decorators\n  ```\n* ```shell\n  go run github.com/MaximZayats/godi/cmd/godi --help\n  ```\n\n## Example\n\n* **Simple**: Getting from container\n    ```go\n    package main\n    \n    import (\n        \"fmt\"\n        \"github.com/MaximZayats/godi/di\"\n    )\n    \n    func main() {\n        di.AddSingletonByFactory[float32](func(c *di.Container) float32 {\n            return 11.22\n        })\n    \n        di.AddInstance[int](123)\n    \n        di.AddByFactory[string](func(c *di.Container) string {\n            return \"aabbcc\"\n        })\n    \n        fmt.Println(di.Get[int]())     // 123\n        fmt.Println(di.Get[string]())  // \"aabbcc\"\n        fmt.Println(di.Get[float32]()) // 11.22\n        fmt.Println(di.Get[float32]()) // 11.22\n    }\n    ```\n\n* **Injection**: Pass arguments to function (*Simple code generation is required*)\n\n  **Full code**: [godi-fiber-example](https://github.com/MaximZayats/godi-fiber-example)\n\n  **Snippet**:\n  ```go\n  type H = func(*fiber.Ctx) error\n\n  // `stringFromDI` will be injected into the handler\n  func handler(c *fiber.Ctx, stringFromDI string) error {\n      return c.SendString(\"Hello from di: \" + stringFromDI)\n  }\n  \n  func main() {\n      di.AddInstance[string](\"I'm string from DI!!!\", c)\n      ...\n      app.Get(\"/\", injection.Inject[H](handler))\n  }\n  ```\n\n[Other examples](/examples)\n\n## Usage\n\n1. Getting from container:\n   * See examples above\n\n2. Injection (decorating):\n   * Generate package for storing decorators:\n     * ```shell\n       go run github.com/MaximZayats/godi/cmd/godi init ./storage/decorators\n       ```\n     * Configure `godi.injection`:\n       * ```go\n         import (\n             \".../storage/decorators\"\n             \"github.com/MaximZayats/godi/injection\"\n         )\n         \n         injection.Configure(decorators.Config)\n         ```\n     * Use injection:\n       * ```go\n         // `a` and `b` will be auto injected in the function\n         func Handler(c context.Context, a int, b string) int {\n             fmt.Println(c, a, b)\n             return a\n         }\n         \n         injection.Configure(decorators.Config)\n         \n         // H is the type alias for the function after injection\n         type H = func(context.Context) int\n         decoratedHandler := injection.Inject[H](Handler)\n         \n         // IMPORTANT! You need to verify injection\n         injection.MustVerifyInjections()\n         \n         decoratedHandler(context.TODO())\n         ```\n         [See full example](examples/pkg/inject.go)\n\n## Benchmarks\n\n[Code](/benchmark/local_container_test.go)\n\n```text\ngoos: windows\ngoarch: amd64\npkg: github.com/MaximZayats/godi/benchmark\ncpu: AMD Ryzen 5 1600 Six-Core Processor\nBenchmarkGetFromFactorySingleton\nBenchmarkGetFromFactorySingleton-12     500488393                2.443 ns/op\nBenchmarkGetInstance\nBenchmarkGetInstance-12                 495795447                2.403 ns/op\nBenchmarkGetFromFactory\nBenchmarkGetFromFactory-12              361722957                3.273 ns/op\nPASS\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaksimzayats%2Fgodi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaksimzayats%2Fgodi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaksimzayats%2Fgodi/lists"}