{"id":14779803,"url":"https://github.com/tiendc/autowire","last_synced_at":"2026-04-09T04:32:48.247Z","repository":{"id":255446664,"uuid":"851652566","full_name":"tiendc/autowire","owner":"tiendc","description":"Dependency injection for Go using generics and reflection","archived":false,"fork":false,"pushed_at":"2024-09-11T11:02:17.000Z","size":38,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-09-14T22:37:23.991Z","etag":null,"topics":["dependency","dependency-injection","dependency-injection-container","wiring"],"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/tiendc.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":"2024-09-03T13:38:31.000Z","updated_at":"2025-08-13T06:05:58.000Z","dependencies_parsed_at":"2024-09-05T07:07:25.384Z","dependency_job_id":"e1007e8b-b95a-4da0-b9c0-c22eaffade8d","html_url":"https://github.com/tiendc/autowire","commit_stats":null,"previous_names":["tiendc/autowire"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tiendc/autowire","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fautowire","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fautowire/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fautowire/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fautowire/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tiendc","download_url":"https://codeload.github.com/tiendc/autowire/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tiendc%2Fautowire/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31586403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-08T14:31:17.711Z","status":"online","status_checked_at":"2026-04-09T02:00:06.848Z","response_time":112,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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","dependency-injection","dependency-injection-container","wiring"],"created_at":"2024-09-17T01:01:00.006Z","updated_at":"2026-04-09T04:32:48.226Z","avatar_url":"https://github.com/tiendc.png","language":"Go","funding_links":[],"categories":["杂项","Recently Updated","Miscellaneous","Microsoft Office"],"sub_categories":["依赖注入","[Sep 15, 2024](/content/2024/09/15/README.md)","Dependency Injection"],"readme":"[![Go Version][gover-img]][gover] [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![GoReport][rpt-img]][rpt]\n\n# Dependency injection for Go 1.18+ using Generics and reflection\n\n## Functionalities\n\n- Automatically wiring/injecting objects on creation.\n- Easy to use (see Usage section).\n- `Shared mode` by default that creates only one instance for a type (this option is configurable).\n- Ability to overwrite objects of specific types on building which is convenient for unit testing.\n- No code generation.\n\n## Installation\n\n```shell\ngo get github.com/tiendc/autowire\n```\n\n## Usage\n\n### General usage\n\n```go\n    // Suppose you have ServiceA and a creator function\n    type ServiceA interface {}\n    func NewServiceA(srvB ServiceB, repoX RepoX) ServiceA {\n        return \u003cServiceA-instance\u003e\n    }\n\n    // and ServiceB\n    type ServiceB interface {}\n    func NewServiceB(ctx context.Context, repoX RepoX, repoY RepoY) (ServiceB, error) {\n        return \u003cServiceB-instance\u003e, nil\n    }\n\n    // and RepoX\n    type RepoX interface {}\n    func NewRepoX(s3Client S3Client) (RepoX, error) {\n        return \u003cRepoX-instance\u003e, nil\n    }\n\n    // and RepoY\n    type RepoY interface {}\n    func NewRepoY(redisClient RedisClient) RepoY {\n        return \u003cRepoY-instance\u003e\n    }\n\n    // and a struct provider\n    type ProviderStruct struct {\n        RedisClient RedisClient\n        S3Client    S3Client\n    }\n\n    var (\n        // init the struct value\n        providerStruct = \u0026ProviderStruct{...}\n\n        // create a container with passing providers\n        container = MustNewContainer([]any{\n            // Service providers\n            NewServiceA,\n            NewServiceB,\n            // Repo providers\n            NewRepoX,\n            NewRepoY,\n            // Struct provider (must be a pointer)\n            providerStruct,\n        })\n    )\n\n    func main() {\n        // Update some values of the struct provider\n        providerStruct.RedisClient = newRedisClient()\n        providerStruct.S3Client = newS3Client()\n\n        // Create ServiceA\n        serviceA, err := autowire.BuildWithCtx[ServiceA](ctx, container)\n        // Create RepoX\n        repoX, err := autowire.Build[RepoX](container)\n    }\n```\n\n### Non-shared mode\n\n```go\n    // Set sharedMode when create a container\n    container = MustNewContainer([]any{\n        // your providers\n    }, SetSharedMode(false))\n\n    // Activate non-shared mode inline for a specific build only\n    serviceA, err := Build[ServiceA](container, NonSharedMode())\n```\n\n### Overwrite values of specific types\n\nThis is convenient in unit testing to overwrite specific types only.\n\n```go\n    // In unit testing, you may want to overwrite some `repos` and `clients` with fake instances.\n    // NOTE: you may need to use `non-shared mode` to make sure cached objects are not used.\n    serviceA, err := Build[ServiceA](container, NonSharedMode(),\n            ProviderOverwrite[RepoX](fakeRepoX),\n            ProviderOverwrite[RepoY](fakeRepoY))\n    serviceA, err := Build[ServiceA](container, NonSharedMode(),\n            ProviderOverwrite[RedisClient](fakeRedisClient),\n            ProviderOverwrite[S3Client](fakeS3Client))\n```\n\n### Reclaim memory after use\n\nTypically, dependency injection is only used at the initialization phase of a program.\nHowever, it can take some space in memory which will become wasted after the phase.\nUse the below trick to reclaim the memory taken by the autowire variables.\n\n```go\n    var (\n        // create a global container\n        container = MustNewContainer(...)\n    )\n\n    func autowireCleanUp() {\n        // Assign nil to allow the garbage collector to reclaim the taken memory\n        container = nil\n    }\n\n    func main() {\n        // Initialization phase\n        // Create services and use them\n        serviceA, _ := autowire.Build[ServiceA](container)\n        serviceB, _ := autowire.Build[ServiceB](container)\n\n        // Clean up the usage, make sure you won't use the vars any more\n        autowireCleanUp()\n    }\n```\n\n## Contributing\n\n- You are welcome to make pull requests for new functions and bug fixes.\n\n## License\n\n- [MIT License](LICENSE)\n\n[doc-img]: https://pkg.go.dev/badge/github.com/tiendc/autowire\n[doc]: https://pkg.go.dev/github.com/tiendc/autowire\n[gover-img]: https://img.shields.io/badge/Go-%3E%3D%201.18-blue\n[gover]: https://img.shields.io/badge/Go-%3E%3D%201.18-blue\n[ci-img]: https://github.com/tiendc/autowire/actions/workflows/go.yml/badge.svg\n[ci]: https://github.com/tiendc/autowire/actions/workflows/go.yml\n[cov-img]: https://codecov.io/gh/tiendc/autowire/branch/main/graph/badge.svg\n[cov]: https://codecov.io/gh/tiendc/autowire\n[rpt-img]: https://goreportcard.com/badge/github.com/tiendc/autowire\n[rpt]: https://goreportcard.com/report/github.com/tiendc/autowire\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiendc%2Fautowire","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftiendc%2Fautowire","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftiendc%2Fautowire/lists"}