{"id":17760834,"url":"https://github.com/sithira/go_autowired","last_synced_at":"2026-02-10T05:31:17.507Z","repository":{"id":259081594,"uuid":"876246932","full_name":"Sithira/go_autowired","owner":"Sithira","description":"A Simple yet powerful Dependency container for Go","archived":false,"fork":false,"pushed_at":"2024-11-13T19:01:58.000Z","size":159,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-25T12:52:58.926Z","etag":null,"topics":["dependency-injection","golang","golang-library","golang-pacakge"],"latest_commit_sha":null,"homepage":"https://github.com/Sithira/go_autowired","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/Sithira.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-10-21T16:37:36.000Z","updated_at":"2024-10-28T03:36:40.000Z","dependencies_parsed_at":"2024-10-23T00:39:06.226Z","dependency_job_id":null,"html_url":"https://github.com/Sithira/go_autowired","commit_stats":null,"previous_names":["sithira/go_autowired"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sithira%2Fgo_autowired","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sithira%2Fgo_autowired/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sithira%2Fgo_autowired/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sithira%2Fgo_autowired/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sithira","download_url":"https://codeload.github.com/Sithira/go_autowired/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253804273,"owners_count":21967046,"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","golang","golang-library","golang-pacakge"],"created_at":"2024-10-26T19:13:35.943Z","updated_at":"2026-02-10T05:31:17.311Z","avatar_url":"https://github.com/Sithira.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @AutoWired for Go 🚀\n\n\u003cimg alt=\"autowired_golang\" height=\"300\" src=\"/img/golang_autowired_logo.webp\" title=\"GoLang Autowired\" width=\"300\"/\u003e\n\n`autowired` is a powerful and flexible dependency injection container for Go applications. It provides a robust set of\nfeatures for managing dependencies, including lifecycle management, scoping, and automatic wiring of structs. This\npackage is designed to simplify dependency management in complex Go applications while maintaining type safety and\nallowing for flexible configuration.\n\n## Features\n\n- Dependency registration with custom names and scopes\n- Support for Singleton, Prototype, and Request scopes\n- Lifecycle hooks (OnInit, OnStart, OnDestroy)\n- Automatic dependency resolution with circular dependency detection\n- Type-safe wrappers for common operations\n- Struct field auto-wiring\n- Thread-safe operations\n- Lazy dependency resolution\n\n## Installation\n\nTo use `autowired` in your Go project, you can install it using:\n\n```bash\ngo get github.com/Sithira/go_autowired\n```\n\n## Usage\n\n### Creating a Container\n\n```go\ncontainer := autowired.NewContainer()\n```\n\n### Registering Dependencies\n\nYou can register dependencies with different scopes:\n\n```go\n// Singleton (default)\nerr := autowired.Register[MyService](container, func () *MyService {\nreturn \u0026MyService{}\n})\n\n// Prototype\nerr := autowired.Register[MyPrototypeService](container, func () *MyPrototypeService {\nreturn \u0026MyPrototypeService{}\n}, autowired.Prototype)\n\n// Request\nerr := autowired.Register[MyRequestService](container, func () *MyRequestService {\nreturn \u0026MyRequestService{}\n}, autowired.Request)\n```\n\n### Resolving Dependencies\n\n```go\nservice, err := autowired.Resolve[*MyService](container)\nif err != nil {\n// Handle error\n}\n```\n\n### Auto-wiring Structs\n\n```go\ntype MyApp struct {\nService *MyService `autowire:\"\"`\n}\n\napp := \u0026MyApp{}\nerr := autowired.AutoWire(container, app)\nif err != nil {\n// Handle error\n}\n```\n\n### Using Scoped Dependencies\n\n#### Singleton Scope (Default)\n\nSingleton-scoped dependencies are created once and reused for all resolutions:\n\n```go\ntype Counter struct {\ncount int\n}\n\nfunc (c *Counter) Increment() {\nc.count++\n}\n\nerr := autowired.Register[Counter](container, func () *Counter {\nreturn \u0026Counter{}\n})\n\ncounter1, _ := autowired.Resolve[*Counter](container)\ncounter1.Increment()\n\ncounter2, _ := autowired.Resolve[*Counter](container)\ncounter2.Increment()\n\nfmt.Println(counter1.count) // Output: 2\nfmt.Println(counter2.count) // Output: 2 (same instance)\n```\n\n#### Prototype Scope\n\nPrototype-scoped dependencies are created anew for each resolution:\n\n```go\nerr := autowired.Register[Counter](container, func () *Counter {\nreturn \u0026Counter{}\n}, autowired.Prototype)\n\ncounter1, _ := autowired.Resolve[*Counter](container)\ncounter1.Increment()\n\ncounter2, _ := autowired.Resolve[*Counter](container)\ncounter2.Increment()\n\nfmt.Println(counter1.count) // Output: 1\nfmt.Println(counter2.count) // Output: 1 (different instance)\n```\n\n#### Request Scope\n\nRequest-scoped dependencies are created once per goroutine (typically per HTTP request):\n\n```go\nerr := autowired.Register[RequestContext](container, func () *RequestContext {\nreturn \u0026RequestContext{}\n}, autowired.Request)\n\nhttp.HandleFunc(\"/\", func (w http.ResponseWriter, r *http.Request) {\nctx, _ := autowired.Resolve[*RequestContext](container)\n// Use ctx...\n\ndefer container.ClearRequestScoped()\n})\n```\n\n### Lifecycle Hooks\n\nYou can define lifecycle hooks for your dependencies:\n\n```go\nhooks := autowired.LifecycleHooks[*MyService]{\nOnInit: func (s *MyService) error {\nfmt.Println(\"Initializing MyService\")\nreturn nil\n},\nOnStart: func (s *MyService) error {\nfmt.Println(\"Starting MyService\")\nreturn nil\n},\nOnDestroy: func (s *MyService) error {\nfmt.Println(\"Destroying MyService\")\nreturn nil\n},\n}\n\nerr := autowired.Register[MyService](container, func () *MyService {\nreturn \u0026MyService{}\n}, hooks)\n```\n\n### Handling Circular Dependencies\n\nThe container automatically detects circular dependencies:\n\n```go\nerr := autowired.Register[ServiceA](container, func (b *ServiceB) *ServiceA {\nreturn \u0026ServiceA{B: b}\n})\n\nerr := autowired.Register[ServiceB](container, func (a *ServiceA) *ServiceB {\nreturn \u0026ServiceB{A: a}\n})\n\n// This will return an error due to circular dependency\n_, err := autowired.Resolve[*ServiceA](container)\nif err != nil {\nfmt.Println(\"Circular dependency detected:\", err)\n}\n```\n\n### Custom Naming\n\nYou can register dependencies with custom names:\n\n```go\nerr := autowired.Register[MyService](container, func () *MyService {\nreturn \u0026MyService{}\n}, \"customName\")\n\nservice, err := autowired.Resolve[*MyService](container, \"customName\")\n```\n\n### Container Cleanup\n\nDon't forget to clean up the container when you're done:\n\n```go\nerr := container.Destroy()\nif err != nil {\n// Handle error\n}\n```\n\n## Best Practices\n\n1. Use Singleton scope for stateless services or when you want to share state across the application.\n2. Use Prototype scope when you need a fresh instance each time or for stateful services that shouldn't share state.\n3. Use Request scope in web applications for request-specific data.\n4. Always handle errors returned by Register, Resolve, and AutoWire.\n5. Use lifecycle hooks for proper resource management.\n6. Avoid circular dependencies in your application design.\n\n## Thread Safety\n\nThe `autowired` package is designed to be thread-safe, allowing for concurrent use in multithreaded applications.\nHowever, be mindful of the thread safety of the dependencies you're managing within the container.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n\n## Support\n\nIf you find this plugin useful, show your support by:\n\n- Giving it a ⭐️ on [GitHub](https://github.com/Sithira/go_autowired)\n- Leaving a like on Pub\n- Showing some ♥️ and buying me a coffee via USDT-TR20 at this address: `TNuTkL1ZJGu2xntmtzHzSiH5YdVqUeAujr`\n\n**Enjoy the plugin!**  \nSithira ✌️","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsithira%2Fgo_autowired","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsithira%2Fgo_autowired","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsithira%2Fgo_autowired/lists"}