{"id":13693628,"url":"https://github.com/Xuanwo/gg","last_synced_at":"2025-05-02T21:32:41.734Z","repository":{"id":46247757,"uuid":"400428879","full_name":"Xuanwo/gg","owner":"Xuanwo","description":"General Golang Code Generator","archived":false,"fork":false,"pushed_at":"2023-06-23T15:19:32.000Z","size":57,"stargazers_count":198,"open_issues_count":1,"forks_count":15,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-07T14:12:58.925Z","etag":null,"topics":["code-generation","codegen","golang-library"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Xuanwo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2021-08-27T07:40:12.000Z","updated_at":"2025-03-03T13:29:51.000Z","dependencies_parsed_at":"2024-01-13T22:55:06.871Z","dependency_job_id":"8e64460b-2298-489e-8563-b4db6654ece2","html_url":"https://github.com/Xuanwo/gg","commit_stats":null,"previous_names":["xuanwo/go-codegen"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Xuanwo%2Fgg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Xuanwo%2Fgg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Xuanwo%2Fgg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Xuanwo%2Fgg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Xuanwo","download_url":"https://codeload.github.com/Xuanwo/gg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252109097,"owners_count":21696193,"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","codegen","golang-library"],"created_at":"2024-08-02T17:01:14.177Z","updated_at":"2025-05-02T21:32:40.683Z","avatar_url":"https://github.com/Xuanwo.png","language":"Go","funding_links":[],"categories":["开源类库","Open source library"],"sub_categories":["代码生成","Code Generation"],"readme":"[![Build Status](https://github.com/Xuanwo/gg/workflows/Unit%20Test/badge.svg?branch=master)](https://github.com/Xuanwo/gg/actions?query=workflow%3A%22Unit+Test%22)\n[![Go dev](https://pkg.go.dev/badge/github.com/Xuanwo/gg)](https://pkg.go.dev/github.com/Xuanwo/gg)\n[![License](https://img.shields.io/badge/license-apache%20v2-blue.svg)](https://github.com/Xuanwo/gg/blob/master/LICENSE)\n[![matrix](https://img.shields.io/matrix/xuanwo@gg:matrix.org.svg?logo=matrix)](https://matrix.to/#/#xuanwo@gg:matrix.org)\n\n# gg\n\n`gg` is a General Golang Code Generator: A Good Game to play with Golang.\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t. \"github.com/Xuanwo/gg\"\n)\n\nfunc main() {\n\tf := NewGroup()\n\tf.AddPackage(\"main\")\n\tf.NewImport().\n\t\tAddPath(\"fmt\")\n\tf.NewFunction(\"main\").AddBody(\n\t\tString(`fmt.Println(\"%s\")`, \"Hello, World!\"),\n\t)\n\tfmt.Println(f.String())\n}\n```\n\nOutput (after `go fmt`)\n\n```go\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"Hello, World!\")\n}\n```\n\n## Design\n\n`gg` is a general golang code generator that designed for resolving problems exists in the following tools:\n\n- [text/template](https://pkg.go.dev/text/template): Additional syntax, Steep learning curve, Complex logic is difficult to maintain\n- [dave/jennifer](https://github.com/dave/jennifer): Overly abstract APIs, user need to take care about `()`, `,` everywhere.\n- [kubernetes-sigs/kubebuilder](https://github.com/kubernetes-sigs/kubebuilder): Parse data from struct tags/comments, not a general code generator.\n\nIn short, `gg` will provide near-native golang syntax and helpful API so play a good game with Golang. With `gg`, we can generate golang code more easily and understandable.\n\n## Usage\n\n### Package Name\n\n```go\nf := Group()\nf.AddPackage(\"main\")\n// package main\n```\n\n### Imports\n\n```go\nf := Group()\nf.NewImport().\n    AddPath(\"context\").\n\tAddDot(\"math\").\n\tAddBlank(\"time\").\n\tAddAlias(\"x\", \"testing\")\n// import (\n//      \"context\"\n//      . \"math\"\n//      _ \"time\"\n//      x \"testing\"\n// )\n```\n\n### Function\n\n```go\nf := Group()\nf.NewFunction(\"hello\").\n    WithReceiver(\"v\", \"*World\").\n    AddParameter(\"content\", \"string\").\n    AddParameter(\"times\", \"int\").\n    AddResult(\"v\", \"string\").\n    AddBody(gg.String(`return fmt.Sprintf(\"say %s in %d times\", content, times)`))\n// func (v *World) hello(content string, times int) (v string) {\n//  return fmt.Sprintf(\"say %s in %d times\", content, times)\n//}\n```\n\n### Struct\n\n```go\nf := Group()\nf.NewStruct(\"World\").\n    AddField(\"x\", \"int64\").\n    AddField(\"y\", \"string\")\n// type World struct {\n//    x int64\n//    y string\n//}\n```\n\n## Acknowledgement\n\n- `gg` is inspired by [dave/jennifer](https://github.com/dave/jennifer), I borrowed most ideas and some code from it. Nice work!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FXuanwo%2Fgg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FXuanwo%2Fgg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FXuanwo%2Fgg/lists"}