{"id":26863867,"url":"https://github.com/lecongnam/go-wire-di","last_synced_at":"2025-03-31T03:33:02.467Z","repository":{"id":285113790,"uuid":"957106332","full_name":"LeCongNam/go-wire-di","owner":"LeCongNam","description":"Gin with Wire Dependency Inject","archived":false,"fork":false,"pushed_at":"2025-03-29T16:13:54.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-29T16:28:06.325Z","etag":null,"topics":["dependency-injection","gin-framework","golang","google-wire"],"latest_commit_sha":null,"homepage":"","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/LeCongNam.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-29T15:15:54.000Z","updated_at":"2025-03-29T16:13:57.000Z","dependencies_parsed_at":"2025-03-29T16:38:09.988Z","dependency_job_id":null,"html_url":"https://github.com/LeCongNam/go-wire-di","commit_stats":null,"previous_names":["lecongnam/go-wire-di"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeCongNam%2Fgo-wire-di","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeCongNam%2Fgo-wire-di/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeCongNam%2Fgo-wire-di/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LeCongNam%2Fgo-wire-di/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LeCongNam","download_url":"https://codeload.github.com/LeCongNam/go-wire-di/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246413363,"owners_count":20773052,"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","gin-framework","golang","google-wire"],"created_at":"2025-03-31T03:33:01.892Z","updated_at":"2025-03-31T03:33:02.455Z","avatar_url":"https://github.com/LeCongNam.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Hướng dẫn cấu hình Gin  với wire(DI)\n\n\n1. Cài đặt \n\n    Với GIN thì đọc và làm theo trang chủ. chắc chỉ mất vài phút nến sẽ ko  đều cập tại đây xem như bạn phải tự hiểu.\n\n    ```bash\n    go install github.com/google/wire/cmd/wire@latest\n    ```\n\n    chạy: ```go mod tidy``` để cho nó cài  đặt  kiểu  ```npm i ``` bằng không nó báo lỗi rằng package này chưa có ^^\n\n2. Phần cấu  hình \n\n    2.1 Trong file cần cấu hình\n\n    - VD: đây là file user service\n\n    ````go\n    type UserController struct {\n\t    userSrv *services.UserService\n    }\n\n    func NewUserController(userSrv *services.UserService) *UserController {\n        return \u0026UserController{userSrv: userSrv}\n    }\n    ````\n\n    - func NewUserController(userSrv *services.UserService) \n        -  bạn có  để ý  thấy rằng trong parameter của function có 1 biến không? đây  chính xác trong golang là  contructor. vậy thì giống như ngôn ngữ khác thì đây chính là kiểu contructor class base. đúng là khắm lọ. Mình cũng không hiểu sao nó không cho phép viết OOP nhưng nó lại có function contructor giống JS es5 ??? :))))\n         Nếu không có thì WIRE  nó cũng báo lỗi  là không được inject và báo lỗi ngay trong phần khai báo file wire.build()\n\n\n    - Những thứ khác như struct thì hãy dùng con trỏ (Pointer).\n\n    2.2 Giờ là màn kịch hay\n     -  tạo 1 file  go với tên tuỳ ý. vd: wire.go\n\n    ```go\n        // go:build wireinject\n        //go:build wireinject\n        // +build wireinject\n\n        package di_wire\n\n        import (\n            \"test/controllers\"\n            \"test/repo\"\n            \"test/services\"\n\n            \"github.com/google/wire\"\n        )\n\n        func InitUserRouterHandler() *controllers.UserController {\n            panic(wire.Build(\n                repo.NewUserRepo,\n                services.NewUserService,\n                controllers.NewUserController,\n            ))\n        }\n    ```\n\n    - 3 cái comment đầu không phải để cho vui, cũng ko phải thích viết gì viết mà nó phải có  3 dòng này. bằng không  golang  nó  build tào  lao =))) nếu thiếu dòng cuối thì  nó build xong báo duplicate function ngay lập tức. Lý do thì mình cũng chưa tìm hiểu nhưng đại loại vô tình chatgpt nó generate  ra 3 dòng đó và work ngay lập tức =)) Nếu không tin các hạ xoá file wire_gen.go và build thử là biết.  \n\n\n    2.3 cuối cùng là chạy lệnh\n    \n    ```bash\n        wire path_your_wire_file\n    ```\n\n    - nếu thành công thì nó sẽ chỉ ra đường dẫn file. nếu không nó sẽ báo lỗi ở wire.build ở DI nào. bạn buộc phải sửa trước khi build lại\n\n\n\n3. Rồi bạn có thể dùng\n\n    đây  là file router.go\n\n```go \n    userController := di_wire.InitUserRouterHandler() //  Init DI\n\n\tv1 := r.Group(\"/v1\")\n\t{\n\t\tv1.GET(\"/user\", userController.GetUser) // 🛠 Dependency Injection\n\t}\n```\n\n - giờ   thì dùng thôi. Bạn có thắc mắc là nó khác gì nếu tự viết bình thường thì struct cũng xài đc?\n    - Câu trả lời là khác hắn. Nếu là newbie thì mình sẽ giải thích ở đây. Nếu không dùng DI ta phải new theo cú pháp bên  dưới đây\n    \n    ```\n        userController := controller.NewUserController().GetUser\n    ```\n\n    - Nó dài loằng ngoằng. Còn cú pháp trên ta chỉ cần  init 1 thằng kia vào là xài. cóc cần phải biết nó từ đâu đến. Xong. chỉ có vậy thôi.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flecongnam%2Fgo-wire-di","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flecongnam%2Fgo-wire-di","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flecongnam%2Fgo-wire-di/lists"}