{"id":16201680,"url":"https://github.com/bin-huang/newc","last_synced_at":"2025-09-19T08:32:28.920Z","repository":{"id":49366010,"uuid":"516640339","full_name":"Bin-Huang/newc","owner":"Bin-Huang","description":"A code generator that generates constructor code for Golang structures.","archived":false,"fork":false,"pushed_at":"2023-02-02T03:55:50.000Z","size":160,"stargazers_count":33,"open_issues_count":1,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-29T07:04:13.219Z","etag":null,"topics":["code-generation","code-generator","constructor","go","golang","struct"],"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/Bin-Huang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-22T06:42:32.000Z","updated_at":"2024-12-14T07:30:47.000Z","dependencies_parsed_at":"2023-02-17T14:01:03.354Z","dependency_job_id":null,"html_url":"https://github.com/Bin-Huang/newc","commit_stats":null,"previous_names":["bin-huang/make-constructor"],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bin-Huang%2Fnewc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bin-Huang%2Fnewc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bin-Huang%2Fnewc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Bin-Huang%2Fnewc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Bin-Huang","download_url":"https://codeload.github.com/Bin-Huang/newc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233559755,"owners_count":18694220,"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":["code-generation","code-generator","constructor","go","golang","struct"],"created_at":"2024-10-10T09:39:32.991Z","updated_at":"2025-09-19T08:32:23.601Z","avatar_url":"https://github.com/Bin-Huang.png","language":"Go","funding_links":["https://buymeacoffee.com/benn"],"categories":[],"sub_categories":[],"readme":"# newc\n\n\u003e **new**-\\* **c**onstructor generator\n\nDoc: **English** | [中文](README_zh.md)\n\n----------\n\nA code generator that generates constructor code for Golang structures.\n\n## Installation\n\n```bash\ngo install github.com/Bin-Huang/newc@latest\n```\n\n## Usage\n\nAdd a `go:generate` command line to the struct which you want to generate a constructor.\n\n```go\n//go:generate newc\ntype UserService struct {\n\tbaseService\n\tuserRepository *repositories.UserRepository\n\tproRepository  *repositories.ProRepository\n}\n```\n\nAfter executing `go generate ./...` the constructor code generated:\n\n```go\n// constructor_gen.go\n\n// NewUserService Create a new UserService\nfunc NewUserService(baseService baseService, userRepository *repositories.UserRepository, proRepository *repositories.ProRepository) *UserService {\n\treturn \u0026UserService{\n\t\tbaseService:    baseService,\n\t\tuserRepository: userRepository,\n\t\tproRepository:  proRepository,\n\t}\n}\n```\n\nSee [more examples here](https://github.com/Bin-Huang/newc/tree/master/test)\n\n## Usage without manual installation\n\n**Recommended for team collaboration**\n\nWithout manual installation, just add this comment line to the struct. Go will automatically install this tool if missing.\n\n```go\n//go:generate go run github.com/Bin-Huang/newc@v0.8.3\n```\n\nFor example:\n\n```go\n//go:generate go run github.com/Bin-Huang/newc@v0.8.3\ntype UserService struct {\n\tbaseService\n\tuserRepository *repositories.UserRepository\n\tproRepository  *repositories.ProRepository\n}\n```\n\nThis is very useful, especially in teamwork. **It can run without manual installation. It doesn't break the work of other people who don't have installed this tool in collaboration.**\n\n## How to return value instead reference?\n\nAdd `--value` parameter\n\n```go\n//go:generate newc --value\ntype Config struct {\n\tdebug  bool\n}\n```\n\nGenerated code:\n\n```go\n// constructor_gen.go\n\n// NewConfig Create a new Config\nfunc NewConfig(debug bool) Config {\n\treturn Config{\n\t\tdebug:  debug,\n\t}\n}\n```\n\n## How to call an initializer in constructor?\n\n1. Add `--init` parameter\n2. Write an `init` method for the struct\n\n```go\n//go:generate newc --init\ntype Controller struct {\n\tlogger *zap.Logger\n\tdebug  bool\n}\n\nfunc (c *Controller) init() {\n\tc.logger = c.logger.With(zap.String(\"tag\", \"controller-debugger\"))\n\tc.debug = true\n}\n```\n\nGenerated code:\n\n```go\n// constructor_gen.go\n\n// NewController Create a new Controller\nfunc NewController(logger *zap.Logger, debug bool) *Controller {\n\ts := \u0026Controller{\n\t\tlogger: logger,\n\t\tdebug:  debug,\n\t}\n\ts.init()\n\treturn s\n}\n```\n\n## How to ignore some fields when generating constructor code?\n\nAdd a tag `newc:\"-\"` to fields that need to be ignored\n\n```go\ntype Forbidden struct {\n\tMsg    string\n\tStatus int    `newc:\"-\"`\n}\n```\n\nGenerated code:\n\n```go\n// NewForbidden Create a new Forbidden\nfunc NewForbidden(msg string) *Forbidden {\n\treturn \u0026Forbidden{\n\t\tMsg: msg,\n\t}\n}\n```\n\n## If you think the `go:generate` comment is too long...\n\nSome suggestions:\n\n1. Add a code snippest in your editor/IDE for the tool (suggested)\n2. ......\n\n## Features \u0026 Motivation\n\n**1. It makes your code easier to write and maintain**.\n\nWriting and updating constructor code for many structs can be laborious and error-prone, especially if you have a huge codebase. These should be handed over to automatic tools like this tool.\n\nAnd it also works well with these dependency injection tools like [`wire`](https://github.com/google/wire). If you use `wire` in your project, you may need this tool very much.\n\n**2. It takes care of the generated code**.\n\nDon't worry about the imports, variable naming, and code style in the generated code.\n\n**3. It is more suitable for teamwork**.\n\nIt doesn't break the work of other people who don't have installed this tool in collaboration. Go will automatically install this tool if missing.\n\n```go\n//go:generate go run github.com/Bin-Huang/newc@v0.8.3\n```\n\n## Sponsoring\n\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://buymeacoffee.com/benn)\n\n![](./doc/donate.png)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbin-huang%2Fnewc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbin-huang%2Fnewc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbin-huang%2Fnewc/lists"}