{"id":13413443,"url":"https://github.com/vardius/gocontainer","last_synced_at":"2025-10-18T23:21:37.487Z","repository":{"id":57496869,"uuid":"190543009","full_name":"vardius/gocontainer","owner":"vardius","description":"Simple Dependency Injection Container","archived":false,"fork":false,"pushed_at":"2020-03-23T09:12:06.000Z","size":36,"stargazers_count":21,"open_issues_count":0,"forks_count":2,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-24T16:56:04.232Z","etag":null,"topics":["container-registry","dependency-injection","go","golang"],"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/vardius.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["vardius"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2019-06-06T08:18:07.000Z","updated_at":"2025-01-30T06:33:56.000Z","dependencies_parsed_at":"2022-08-28T11:50:44.126Z","dependency_job_id":null,"html_url":"https://github.com/vardius/gocontainer","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vardius%2Fgocontainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vardius%2Fgocontainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vardius%2Fgocontainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vardius%2Fgocontainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vardius","download_url":"https://codeload.github.com/vardius/gocontainer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248280190,"owners_count":21077412,"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":["container-registry","dependency-injection","go","golang"],"created_at":"2024-07-30T20:01:40.518Z","updated_at":"2025-10-18T23:21:37.417Z","avatar_url":"https://github.com/vardius.png","language":"Go","readme":"🪣 gocontainer\n================\n[![Build Status](https://travis-ci.org/vardius/gocontainer.svg?branch=master)](https://travis-ci.org/vardius/gocontainer)\n[![Go Report Card](https://goreportcard.com/badge/github.com/vardius/gocontainer)](https://goreportcard.com/report/github.com/vardius/gocontainer)\n[![codecov](https://codecov.io/gh/vardius/gocontainer/branch/master/graph/badge.svg)](https://codecov.io/gh/vardius/gocontainer)\n[![](https://godoc.org/github.com/vardius/gocontainer?status.svg)](https://pkg.go.dev/github.com/vardius/gocontainer)\n[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/vardius/gocontainer/blob/master/LICENSE.md)\n\n\u003cimg align=\"right\" height=\"180px\" src=\"https://github.com/vardius/gorouter/blob/master/website/src/static/img/logo.png?raw=true\" alt=\"logo\" /\u003e\n\ngocontainer - Dependency Injection Container\n\n📖 ABOUT\n==================================================\nContributors:\n\n* [Rafał Lorenz](http://rafallorenz.com)\n\nWant to contribute ? Feel free to send pull requests!\n\nHave problems, bugs, feature ideas?\nWe are using the github [issue tracker](https://github.com/vardius/gocontainer/issues) to manage them.\n\n## 📚 Documentation\n\nFor __examples__ **visit [godoc#pkg-examples](http://godoc.org/github.com/vardius/gocontainer#pkg-examples)**\n\nFor **GoDoc** reference, **visit [pkg.go.dev](https://pkg.go.dev/github.com/vardius/gocontainer)**\n\n[MustInvokeMany](https://godoc.org/github.com/vardius/gocontainer#example-package--MustInvokeMany)\n\n🚏 HOW TO USE\n==================================================\n\nFirst file `main.go` simply gets the repository from the container and prints it\nwe use **MustInvoke** method to simply present the way where we keep type safety\n```go\npackage main\n\nimport (\n    \"github.com/vardius/gocontainer/example/repository\"\n    \"github.com/vardius/gocontainer\"\n)\n\nfunc main() {\n    gocontainer.MustInvoke(\"repository.mysql\", func(r Repository) {\n        fmt.Println(r)\n    })\n}\n```\nOur database implementation uses `init()` function to register db service\n```go\npackage database\n\nimport (\n    \"fmt\"\n    \"database/sql\"\n\n    \"github.com/vardius/gocontainer\"\n)\n\nfunc NewDatabase() *sql.DB {\n    db, _ := sql.Open(\"mysql\", \"dsn\")\n\n    return db\n}\n\nfunc init() {\n    db := gocontainer.MustGet(\"db\")\n\n    gocontainer.Register(\"db\", NewDatabase())\n}\n```\nOur repository accesses earlier on registered db service\nand following the same patter uses `init()` function to register repository service within container\n```go\npackage repository\n\nimport (\n    \"fmt\"\n    \"database/sql\"\n\n    \"github.com/vardius/gocontainer\"\n    _ \"github.com/vardius/gocontainer/example/database\"\n)\n\ntype Repository interface {}\n\nfunc NewRepository(db *sql.DB) Repository {\n    return \u0026mysqlRepository{db}\n}\n\ntype mysqlRepository struct {\n    db *sql.DB\n}\n\nfunc init() {\n    db := gocontainer.MustGet(\"db\")\n\n    gocontainer.Register(\"repository.mysql\", NewRepository(db.(*sql.DB)))\n}\n```\nYou can disable global container instance by setting `gocontainer.GlobalContainer` to `nil`.\nThis package allows you to create many containers.\n```go\npackage main\n\nimport (\n    \"github.com/vardius/gocontainer/example/repository\"\n    \"github.com/vardius/gocontainer\"\n)\n\nfunc main() {\n    // disable global container instance\n    gocontainer.GlobalContainer = nil\n\n    mycontainer := gocontainer.New()\n    mycontainer.Register(\"test\", 1)\n}\n```\nPlease check [GoDoc](http://godoc.org/github.com/vardius/gocontainer) for more methods and examples.\n\n📜 [License](LICENSE.md)\n-------\n\nThis package is released under the MIT license. See the complete license in the package\n","funding_links":["https://github.com/sponsors/vardius"],"categories":["Miscellaneous","Microsoft Office","其他杂项","杂项","Dependency Injection"],"sub_categories":["Dependency Injection","依赖性注入","依赖注入"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvardius%2Fgocontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvardius%2Fgocontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvardius%2Fgocontainer/lists"}