{"id":16133180,"url":"https://github.com/infogulch/inject","last_synced_at":"2025-07-01T12:38:52.376Z","repository":{"id":57551446,"uuid":"62845610","full_name":"infogulch/inject","owner":"infogulch","description":"Inject is a small dependency injection library for Go","archived":false,"fork":false,"pushed_at":"2024-03-13T20:23:56.000Z","size":23,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-06T15:16:57.420Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/infogulch.png","metadata":{"files":{"readme":"README.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-07-08T00:02:40.000Z","updated_at":"2024-03-24T18:03:43.000Z","dependencies_parsed_at":"2024-06-19T21:12:32.737Z","dependency_job_id":"eed1f761-09e3-492e-91e5-b2cdb4a11b83","html_url":"https://github.com/infogulch/inject","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/infogulch/inject","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infogulch%2Finject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infogulch%2Finject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infogulch%2Finject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infogulch%2Finject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/infogulch","download_url":"https://codeload.github.com/infogulch/inject/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/infogulch%2Finject/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262963963,"owners_count":23391924,"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":[],"created_at":"2024-10-09T22:37:49.342Z","updated_at":"2025-07-01T12:38:52.347Z","avatar_url":"https://github.com/infogulch.png","language":"Go","readme":"# inject\n\n`inject` is a small dependency injection library for Go.\n\n[![GoDoc](https://godoc.org/github.com/infogulch/inject?status.svg)](https://godoc.org/github.com/infogulch/inject)\n\n```shell\ngo get -u github.com/infogulch/inject\n```\n\n## Why?\n\n### Dependency Injection?\n\nDependency Injection (DI) is a software design pattern that promotes loose\ncoupling between components by separating the creation and management of object\ndependencies from the objects themselves. The promise of this pattern is that it\nhelps authors write more modular and testable code.\n\n### DI in Go?\n\nIn the wider programming world, dependency injection typically revolves around\nlanguage level constructs like constructors. Go does not have constructors in\nthat sense, only conventions like the common `New()` function. Consequently,\nusage of `inject` revolves around providing and requesting functions and types.\n\n## How to use\n\n1. Define your dependencies as structs, interfaces, functions that accept and\n   return structs or interfaces, or slices of the previous.\n2. Call `inject.New()`, providing all of your dependencies and constructor funcs\n   as arguments, to get a `var scope *Scope` which implements `Injector`.\n3. Request types with an `Injector`: (Note that Go does not support generic\n   methods, so these must be regular functions.)\n    a. Call the generic `inject.Get` like so: `t, err := inject.Get[T](scope)`\n       to recursively evaluate the constructors to return a `t`, or an error if\n       there was an issue.\n    b. Call `inject.Inject[T](scope, myfunc)` to call the func with arguments\n       sourced from the scope and return the func's return value. The func must\n       return one value, and optionally an error.\n\n## How it works\n\nBuild an injection scope by calling `inject.New` with values, functions, and\nslices. Then call `inject.Get` and `inject.Inject` to get values and call\nfunctions with the types available in the scope.\n\nProvide an object `T` and it will be returned when requested. Provide any\nfunction that returns a `U` (or a `(U, error)`), then that function will be\ncalled to produce a `U` when requested. If a function requires inputs its\narguments will be extracted from the injection scope then called, recursively,\nuntil the originally requested type is returned.\n\nEach type can only have one provider, that is, you can't have two functions that\nboth return `T`, or provide a `T` directly and a `func() T`, etc. The exception\nto this is slices, which are concatenated together if more than one are\nprovided. Like all types, only one of any specific primitive type is allowed. To\nget around this, name your type like `type MyInt int`.\n\nThe exception to the \"one type one provider\" rule is slices. Adding multiple\nslices of the same type (or slices of functions that return the same type)\nresults the scope containing one slice that is the concatenation of the provided\nslices.\n\n### Example\n\nSee the [example](./example/main.go) for a working example http server\napplication that demonstrates middleware, repository, and handler patterns.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfogulch%2Finject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finfogulch%2Finject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finfogulch%2Finject/lists"}