{"id":20168867,"url":"https://github.com/quangtung97/svloc","last_synced_at":"2025-04-10T02:05:58.708Z","repository":{"id":200409350,"uuid":"704322038","full_name":"QuangTung97/svloc","owner":"QuangTung97","description":"A DI Container alternative for Go","archived":false,"fork":false,"pushed_at":"2024-02-12T14:02:51.000Z","size":89,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-16T00:16:56.411Z","etag":null,"topics":["dependency-injection","go","golang","static-typing"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/QuangTung97/svloc","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/QuangTung97.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}},"created_at":"2023-10-13T02:38:47.000Z","updated_at":"2024-02-27T04:40:40.000Z","dependencies_parsed_at":"2024-02-12T15:21:05.847Z","dependency_job_id":"5fe58fb7-1acb-4844-95fe-520e2d1a5244","html_url":"https://github.com/QuangTung97/svloc","commit_stats":null,"previous_names":["quangtung97/svloc"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuangTung97%2Fsvloc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuangTung97%2Fsvloc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuangTung97%2Fsvloc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/QuangTung97%2Fsvloc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/QuangTung97","download_url":"https://codeload.github.com/QuangTung97/svloc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224547298,"owners_count":17329427,"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","go","golang","static-typing"],"created_at":"2024-11-14T01:10:24.541Z","updated_at":"2024-11-14T01:10:24.965Z","avatar_url":"https://github.com/QuangTung97.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A Library for Dependency Injection in Go\n\n[![svloc](https://github.com/QuangTung97/svloc/actions/workflows/go.yml/badge.svg)](https://github.com/QuangTung97/svloc/actions/workflows/go.yml)\n[![Coverage Status](https://coveralls.io/repos/github/QuangTung97/svloc/badge.svg?branch=master)](https://coveralls.io/github/QuangTung97/svloc?branch=master)\n\n## Why this library?\n\n* It is simpler than [uber/fx](https://github.com/uber-go/fx), yet still powerful\n* Static typing \u0026 without using reflection\n* Easy to use and easy to replace objects for unit \u0026 integration testing (by using **Override**, one can replace any part of the **default** dependency graph)\n* Easy to read \u0026 navigate with IDEs\n* Safe to use in multiple goroutines\n* Very easy-to-read stacktraces when something wrong happens\n* Provide **default** wiring through the **Register** method, or no default wiring through **RegisterEmpty**\n\n#### This library does NOT actually use the Service Locator pattern\n\n### Limitations\n\n* Runtime wiring of objects\n* Deep stack calls\n* ``Override`` functions only work outside of the ``anonymous functions`` in the ``Register`` calls\n## Installtion\n\n```bash\ngo get github.com/QuangTung97/svloc@v0.5.0\n```\n\n## Examples\n\nAssume ``RepoImpl`` implements interface ``Repo``:\n\n```go\npackage main\n\ntype Repo interface {\n\tGetUser() string\n}\n\ntype RepoImpl struct {\n\tname string\n}\n\nfunc NewRepo(name string) *RepoImpl {\n\treturn \u0026RepoImpl{\n\t\tname: name,\n\t}\n}\n\nfunc (r *RepoImpl) GetUser() string {\n\treturn r.name\n}\n```\n\nAssume ``Service`` with method ``Hello``:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n)\n\ntype Service struct {\n\trp Repo\n}\n\nfunc NewService(repo Repo) *Service {\n\treturn \u0026Service{\n\t\trp: repo,\n\t}\n}\n\nfunc (s *Service) Hello() {\n\tfmt.Println(\"Hello User:\", s.rp.GetUser())\n}\n```\n\nWe can create ``Locator[T]`` objects:\n\n```go\npackage main\n\nimport (\n\t\"github.com/QuangTung97/svloc\"\n)\n\nvar usernameLoc = svloc.RegisterEmpty[string]()\n\nvar repoLoc = svloc.Register[Repo](func(unv *svloc.Universe) Repo {\n\treturn NewRepo(\n\t\tusernameLoc.Get(unv),\n\t)\n})\n\nvar serviceLoc = svloc.Register[*Service](func(unv *svloc.Universe) *Service {\n\treturn NewService(repoLoc.Get(unv))\n})\n```\n\nThe 3 newly created objects: ``usernameLoc``, ``repoLoc``, ``serviceLoc``\nare all immutable objects and safe to use concurrently.\n\nThe ``svloc.PreventRegistering`` will not allow ``Register`` functions to be called after that point.\nUsually at the start of the ``main()`` function.\n\nTo use in ``main()``, first creates a new ``Universe``.\nThen call ``MustOverride()`` on ``usernameLoc`` to provide the username string.\nAnd then call the ``serviceLoc.Get()`` with that ``Universe``,\nAll of the wiring will happen automatically:\n\n```go\npackage main\n\nfunc main() {\n\tsvloc.PreventRegistering()\n\n\tunv := svloc.NewUniverse()\n\tusernameLoc.MustOverride(unv, \"user01\")\n\n\tsvc := serviceLoc.Get(unv)\n\tsvc.Hello()\n}\n```\n\nFull example:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/QuangTung97/svloc\"\n)\n\ntype Repo interface {\n\tGetUser() string\n}\n\ntype RepoImpl struct {\n\tname string\n}\n\nfunc NewRepo(name string) *RepoImpl {\n\treturn \u0026RepoImpl{\n\t\tname: name,\n\t}\n}\n\nfunc (r *RepoImpl) GetUser() string {\n\treturn r.name\n}\n\ntype Service struct {\n\trp Repo\n}\n\nfunc NewService(repo Repo) *Service {\n\treturn \u0026Service{\n\t\trp: repo,\n\t}\n}\n\nfunc (s *Service) Hello() {\n\tfmt.Println(\"Hello User:\", s.rp.GetUser())\n}\n\nvar usernameLoc = svloc.RegisterEmpty[string]()\n\nvar repoLoc = svloc.Register[Repo](func(unv *svloc.Universe) Repo {\n\treturn NewRepo(\n\t\tusernameLoc.Get(unv),\n\t)\n})\n\nvar serviceLoc = svloc.Register[*Service](func(unv *svloc.Universe) *Service {\n\treturn NewService(repoLoc.Get(unv))\n})\n\nfunc main() {\n\tsvloc.PreventRegistering()\n\n\tunv := svloc.NewUniverse()\n\tusernameLoc.MustOverride(unv, \"user01\")\n\n\tsvc := serviceLoc.Get(unv)\n\tsvc.Hello()\n}\n```\n\nUsing ``OnShutdown`` and ``Shutdown``:\n\n```go\npackage main\n\nvar repoLoc = svloc.Register[Repo](func(unv *svloc.Universe) Repo {\n\tunv.OnShutdown(func() {\n\t\tfmt.Println(\"Shutdown Repo\")\n\t})\n\n\treturn NewRepo(\n\t\tusernameLoc.Get(unv),\n\t)\n})\n\nvar serviceLoc = svloc.Register[*Service](func(unv *svloc.Universe) *Service {\n\tunv.OnShutdown(func() {\n\t\tfmt.Println(\"Shutdown Service\")\n\t})\n\treturn NewService(repoLoc.Get(unv))\n})\n\nfunc main() {\n\tsvloc.PreventRegistering()\n\n\tunv := svloc.NewUniverse()\n\tdefer unv.Shutdown()\n\n\tusernameLoc.MustOverride(unv, \"user01\")\n\n\tsvc := serviceLoc.Get(unv)\n\tsvc.Hello()\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquangtung97%2Fsvloc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquangtung97%2Fsvloc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquangtung97%2Fsvloc/lists"}