{"id":19765654,"url":"https://github.com/cold-bin/wire-example","last_synced_at":"2025-10-10T23:17:17.697Z","repository":{"id":218204799,"uuid":"745862341","full_name":"cold-bin/wire-example","owner":"cold-bin","description":"wire的示例","archived":false,"fork":false,"pushed_at":"2024-01-20T11:39:21.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-11T00:12:45.827Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cold-bin.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2024-01-20T11:24:07.000Z","updated_at":"2024-01-20T11:25:48.000Z","dependencies_parsed_at":"2024-01-20T12:45:01.601Z","dependency_job_id":null,"html_url":"https://github.com/cold-bin/wire-example","commit_stats":null,"previous_names":["cold-bin/wire-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cold-bin%2Fwire-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cold-bin%2Fwire-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cold-bin%2Fwire-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cold-bin%2Fwire-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cold-bin","download_url":"https://codeload.github.com/cold-bin/wire-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241096358,"owners_count":19908969,"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-11-12T04:18:49.735Z","updated_at":"2025-10-10T23:17:17.560Z","avatar_url":"https://github.com/cold-bin.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## introduce\n\n本项目仅仅只是一个示例\n\n使用wire实现依赖注入，避免层间依赖，既方便mock和测试，也方便维护\n\n## 项目结构\n\n```\n.\n├── cmd\n│   └── di # injector\n├── conf\n├── dal\n├── entity\n├── handler\n├── provider # di所需的provider\n│   ├── kmq\n│   ├── mysql\n│   ├── redis\n│   └── zap\n├── router\n└── service\n```\n\n## 食用指南\n\n- 每一层都需要将依赖外部组件和下一层抽象到一个结构体里，然后每层（router,handler,service,dal）都按照业务拆分成实体并封装实体方法\n- 这样所有的依赖都可以通过`wire`注入，而不是直接引用外部包的变量\n- `wire`提供了一些很方便的api，帮助我们省掉工厂方法的创建，诸如`wire.Struct`等\n- 但是有一些组件的对象，只有我们自己提供工厂方法来创建了，例如数据库句柄等\n\n### injector\n\n```go\n//go:build wireinject\n// +build wireinject\n\npackage di\n\nimport (\n\t\"github.com/google/wire\"\n\t\"wire-example/dal\"\n\t\"wire-example/handler\"\n\t\"wire-example/provider/kmq\"\n\t\"wire-example/provider/mysql\"\n\t\"wire-example/provider/redis\"\n\t\"wire-example/provider/zap\"\n\t\"wire-example/router\"\n\t\"wire-example/service\"\n)\n\nfunc BuildItemRouter() *router.ItemRouter {\n\tpanic(\n\t\twire.Build(\n\t\t\twire.Struct(new(router.ItemRouter), \"*\"),\n\t\t\twire.Struct(new(handler.ItemHandler), \"*\"),\n\t\t\twire.Struct(new(service.ItemSvc), \"*\"),\n\t\t\tredis.RClient,\n\t\t\tkmq.KmqClient,\n\t\t\tzap.Logger,\n\t\t\twire.Struct(new(dal.ItemDal), \"*\"),\n\t\t\twire.Struct(new(dal.Base), \"*\"),\n\t\t\tmysql.DBClient,\n\t\t))\n}\n```\n\n### provider\n\n自提供工厂方法的provider示例：\n\n```go\nvar (\n\tgdb  *gorm.DB\n\tonce = \u0026sync.Once{}\n)\n\n// 单例模式\nfunc DBClient() *gorm.DB {\n\tif gdb != nil {\n\t\treturn gdb\n\t}\n\n\tonce.Do(func() {\n\t\tsource := fmt.Sprintf(\"%s:%s@(%s:%s)/%s?charset=utf8mb4\u0026parseTime=True\u0026loc=Local\",\n\t\t\tconf.MysqlUsername,\n\t\t\tconf.MysqlPassword,\n\t\t\tconf.MysqlHost,\n\t\t\tconf.MysqlPort,\n\t\t\tconf.MysqlDbname,\n\t\t)\n\t\tdb_, err := gorm.Open(mysql.Open(source), \u0026gorm.Config{\n\t\t\tNamingStrategy: schema.NamingStrategy{\n\t\t\t\tSingularTable: true,\n\t\t\t},\n\t\t})\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tdb, err := db_.DB()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tdb.SetMaxOpenConns(conf.MysqlMaxOpenConns)\n\t\tdb.SetMaxIdleConns(conf.MysqlMaxIdleConns)\n\t\tdb.SetConnMaxLifetime(conf.MysqlConnMaxLifeMinutes * time.Minute)\n\t\terr = db_.Migrator().AutoMigrate()\n\t\tif err != nil {\n\t\t\tpanic(err)\n\t\t}\n\t\tgdb = db_\n\t})\n\treturn gdb\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcold-bin%2Fwire-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcold-bin%2Fwire-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcold-bin%2Fwire-example/lists"}