{"id":13564405,"url":"https://github.com/phcollignon/Go-Template","last_synced_at":"2025-04-03T21:30:47.311Z","repository":{"id":54535644,"uuid":"189964358","full_name":"phcollignon/Go-Template","owner":"phcollignon","description":"Go Template examples and code generator","archived":false,"fork":false,"pushed_at":"2024-07-31T16:05:36.000Z","size":12831,"stargazers_count":76,"open_issues_count":1,"forks_count":42,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-04T17:47:28.711Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phcollignon.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-03T08:21:58.000Z","updated_at":"2024-10-20T20:40:48.000Z","dependencies_parsed_at":"2024-11-04T17:34:28.157Z","dependency_job_id":null,"html_url":"https://github.com/phcollignon/Go-Template","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phcollignon%2FGo-Template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phcollignon%2FGo-Template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phcollignon%2FGo-Template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phcollignon%2FGo-Template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phcollignon","download_url":"https://codeload.github.com/phcollignon/Go-Template/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247082946,"owners_count":20880749,"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-08-01T13:01:30.813Z","updated_at":"2025-04-03T21:30:43.282Z","avatar_url":"https://github.com/phcollignon.png","language":"Go","funding_links":[],"categories":["others","Go"],"sub_categories":[],"readme":"# Introduction\nThis repository contains a Go code generator based on Go Template and a set of Go Template examples.\n                                                  \n\n## Go code generator\nGo templates are one of the best choice to build a fast and small code generation tool. \n\nThe sample code generator tool presented here can generate any type of code from Go templates and json or yaml data. \n\n![alt text](Gocodegen.png \"Logo Title Text 1\")\n\n\n# Installation\nDownload zip archive from github or clone this repository\n\n`git clone https://github.com/phcollignon/Go-Template`\n\n\n# Usage\nYou can use it as a command line or as a go library to dynamically generate code in go programs.\n\n## build the binary\nGo in `Go-Template/src` directory.\n\n```\ncd Go-Template/src\ngo build\n```\n\nAdd Go-Template to your binaries or to your PATH (i.e `sudo mv Go-Template /usr/local/bin`)\n\n## command line\nGo in one of the examples directory and run Go-Template with a data file and a template file as parameters.\n\n```\ncd Go-Template/examples/01-values\nGo-Template -d email.json -t  email.tpl\n```\n\n## output directory \n\nBy default, generated files are created in the same folder as the json file.  \n\nYou can change the ouput directory with `-o outputdirectory`\n\n`Go-Template -d email.json -t  email.tpl -o /tmp/gocodegentest`\n\n## multiple file generation\nIf your Json Data file is designed to generate multiple files (see Example 5 below), use the following command :\n\n`Go-Template -d mailing.json -t mailing.tpl -m multi`\n\n# Examples\n\nWe provide here some code generation examples, please note that gocodgen is based on go templates, so for more advanced features, you can look at go template documentation.\n\n## 01 :  Simple code generation\n\nLet's suppose the following email template :\n\n```\nDear {{.Name}},\n\nHello, \n\nWe would like to assign some tasks for {{.Project}} project :\n{{range .Topics}}\n    - {{.}}\n{{end}}\n\nCan we plan a meeting on {{.Date}} ?\n\nRegards,\n```\n\nAnd the following data :\n\n```\n{\n    \"Name\": \"Phil\",\n    \"Date\": \"01/01/2018\",\n    \"Project\": \"Go-Template\",\n    \"Topics\": [\n        \"write documentation\",\n        \"publish to github.com\",\n        \"add more examples\"\n    ]\n}\n\n```\n\nThe following command,\n\n`Go-Template -d 01-values/email.json -t 01-values/email.tpl`\n\ngenerates `email.generated.txt` file\n\n```\n\nDear Phil,\n\nHello, \n\nWe would like to assign some tasks for Go-Template project :\n\n    - write documentation\n\n    - publish to github.com\n\n    - add more examples\n\n\nCan we plan a meeting on 01/01/2018 ?\n\nRegards,\n\n```\n\n\n## 02 :  Loop on Json Data\n\nLet's suppose the following template to generate SQL code for table create :\n\n```\n{{range  .Schema.Table}}\n\tCREATE TABLE {{ .Name}} (\n\t\t{{range $idx, $column := .Column}}\n\t\t\t{{if  $idx }} , {{end}}{{ $column.Name}} {{$column.Type}} {{if $column.Size }} {{$column.Size}} {{end}}\n\t\t{{end}}\n\t\t)\n{{end}}\n\n```\n\nAnd the following schema definition :\n\n```\n{\n  \"Schema\": {\n    \"Table\": [\n      {\n        \"Name\": \"EMPLOYEE\",\n        \"Column\": [\n          {\n            \"Name\": \"ID\",\n            \"Type\": \"INTEGER\"\n          },\n          {\n            \"Name\": \"FIRSTNAME\",\n            \"Type\": \"VARCHAR\",\n            \"Size\": \"256\"\n          },\n          {\n            \"Name\": \"LASTNAME\",\n            \"Type\": \"VARCHAR\",\n            \"Size\": \"256\"\n          },\n          {\n            \"Name\": \"AGE\",\n            \"Type\": \"INTEGER\"\n          }\n        ]\n      },\n      {\n        \"Name\": \"PRODUCT\",\n        \"Column\": [\n          {\n            \"Name\": \"ID\",\n            \"Type\": \"INTEGER\"\n          },\n          {\n            \"Name\": \"NAME\",\nThe following command,\n\n            \"Type\": \"VARCHAR\",\n            \"Size\": \"256\"\n          },\n          {\n            \"Name\": \"PRICE\",\n            \"Type\": \"REAL\"\n          }\n        ]\n      }\n    ]\n  }\n}\n\n```\n\n`Go-Template -d schema.json -t createtable.tpl`\n\ngenerates the following file\n\n```\n\tCREATE TABLE EMPLOYEE (\t\n\t\t\tID INTEGER \t\n\t\t\t , FIRSTNAME VARCHAR  256 \t\t\n\t\t\t , LASTNAME VARCHAR  256 \t\t\n\t\t\t , AGE INTEGER \t\t\n\t\t)\n\n\tCREATE TABLE PRODUCT (\t\t\n\t\t\tID INTEGER \t\t\n\t\t\t , NAME VARCHAR  256 \t\t\n\t\t\t , PRICE REAL \t\t\n\t\t)\n```\n\n## 03 :  Add conditions and Golang template functions\n\nLet's imagine you want to compare some numbers :\n\n```\n{{ range .Number}}\n    {{if eq  .n1 .n2 }}\n        {{- .n1}} = {{.n2}}\n    {{else}}\n        {{- if lt .n1 .n2 }}\n            {{- .n1}} \u003c {{.n2}}\n        {{else}}\n            {{- .n1}} \u003e {{.n2}}\n        {{end}}\n    {{end}}\n{{end}}\n\n```\n\nAnd the following numbers to be compared :\n\n```\n{\n    \"Number\" : [\n        {\"n1\":1,\"n2\":2},\n        {\"n1\":5,\"n2\":3},\n        {\"n1\":2,\"n2\":2},\n        {\"n1\":1,\"n2\":5},\n        {\"n1\":6,\"n2\":6},\n        {\"n1\":5,\"n2\":2}\n\n    ]\n}\n\n```\n\n`Go-Template -d numbers.json -t numbers.tpl`\n\ngenerates the following file\n\n```\n    1 \u003c 2\n    5 \u003e 3\n    2 = 2\n    1 \u003c 5\n    6 = 6\n    5 \u003e 2\n \n```\n## 04 :  Custom functions\n\nLet's now consider a more advanced sample where you want to apply some custome functions in your code generation.\n\nThe JavaBeans setter and getter functions must be written as `getPropertyName()` for `propertyName` property.\n\nFirst, add a custom function in `my-funcs.go` file\n\n\n```\nvar MyFuncMap = map[string]interface{}{\n\t\"ToGetterName\": ToGetterName,\n\t\"ToSetterName\": ToSetterName}\n\nfunc ToGetterName(name string) string {\n\treturn \"get\" + strings.Title(name)\n}\nfunc ToSetterName(name string) string {\n\treturn \"set\" + strings.Title(name)\n}\n\n```\n\nNow you can use that custom function in your Go template :\n\n```\npublic class User implements java.io.Serializable {\n    {{range .User.Property }}\n        private {{.Type}} {{ .Name  }};\n    {{end}}\n\n\n    {{range .User.Property }}\n        public {{.Type}} {{ .Name | ToGetterName }}(){\n            return this.{{.Name}};\n        }\n    {{end}}\n\n    {{range .User.Property }}\n        public void {{ .Name   | ToSetterName   }}({{.Type}} {{.Name}}){\n            this.{{.Name}} = {{.Name}};\n        }\n    {{end}}\n}\n```\n\nEither compile the gocodgen for your platform or install Go and run \n`go run main.go my-funcs.go -d numbers.json -t numbers.tpl`\n\nFor the following json data, \n\n```\n{\n    \"User\": {\n        \"Name\" : \"Person\",\n        \"Property\": [\n            {\n               \"Name\": \"firstName\",\n               \"Type\": \"String\"\n            },\n            {\n                \"Name\": \"lastName\",\n                \"Type\": \"String\"\n            },\n            {\n                \"Name\": \"age\",\n                \"Type\": \"Integer\"\n            }\n        ]\n    }\n}\n \n```\n\nYou get this result :\n\n```\npublic class User implements java.io.Serializable {\n    \n        private String firstName;\n    \n        private String lastName;\n    \n        private Integer age;\n    \n\n\n    \n        public String getFirstName(){\n            return this.firstName;\n        }\n    \n        public String getLastName(){\n            return this.lastName;\n        }\n    \n        public Integer getAge(){\n            return this.age;\n        }\n    \n\n    \n        public void setFirstName(String firstName){\n            this.firstName = firstName;\n        }\n    \n        public void setLastName(String lastName){\n            this.lastName = lastName;\n        }\n    \n        public void setAge(Integer age){\n            this.age = age;\n        }\n    \n}\n\n```\n\n\n\n## 05 :  Generate multiple files\n\nSometime, you want to generate a set of files from some data.\n\nThe most obvious example is a mailing .. let's have the same email as in Example 01\n\n```\nDear {{.Name}},\n\nHello, \n\nWe would like to assign some tasks for {{.Project}} project :\n{{range .Topics}}\n    - {{.}}\n{{end}}\n\nCan we plan a meeting on {{.Date}} ?\n\nRegards,\n\n```\n\nYou include in your Json Data file, a `Files` key containing a table of objects.\nEach object has a `FileName` used to create the file, and a `Data` object which container the code generation data.\n\n```\n{\n    \"Files\": [{\n        \"FileName\": \"test1\",\n        \"Data\": {\n            \"Name\": \"phil\",\n            \"Date\": \"01/01/2018\",\n            \"Project\": \"Go-Template\",\n            \"Topics\": [\n                \"write documentation\",\n                \"publish to github.com\",\n                \"add more examples\"\n            ]\n        }\n    },\n    {\n        \"FileName\": \"test2\",\n        \"Data\": {\n            \"Name\": \"john\",\n            \"Date\": \"01/01/2018\",\n            \"Project\": \"Go-Template\",\n            \"Topics\": [\n                \"write documentation\",\n                \"publish to github.com\",\n                \"add more examples\"\n            ]\n        }\n    },\n    {\n        \"FileName\": \"test3\",\n        \"Data\": {\n            \"Name\": \"peter\",\n            \"Date\": \"01/01/2018\",\n            \"Project\": \"Go-Template\",\n            \"Topics\": [\n                \"write documentation\",\n                \"publish to github.com\",\n                \"add more examples\"\n            ]\n        }\n    }]\n}\n\n```\n\nThe following command (do not forget `-m multi` flag) :\n\n`Go-Template -d mailing.json -t mailing.tpl -m multi`\n\ngenerates 3 different files :\n\ntest1 :\n\n```\nDear phil,\n\nHello, \n\nWe would like to assign some tasks for Go-Template project :\n\n    - write documentation\n\n    - publish to github.com\n\n    - add more examples\n\n\nCan we plan a meeting on 01/01/2018 ?\n\nRegards,\n \n```\n\ntest2 :\n\n```\nDear john,\n\nHello, \n\nWe would like to assign some tasks for Go-Template project :\n\n    - write documentation\n\n    - publish to github.com\n\n    - add more examples\n\n\nCan we plan a meeting on 01/01/2018 ?\n\nRegards,\n\n \n```\n\ntest3 :\n\n```\n\nDear peter,\n\nHello, \n\nWe would like to assign some tasks for Go-Template project :\n\n    - write documentation\n\n    - publish to github.com\n\n    - add more examples\n\n\nCan we plan a meeting on 01/01/2018 ?\n\nRegards,\n\n \n```\n\n## Go templates examples\nHere is the list of Go template examples.  You can use them in your own code or with the generator documented below.\n\n| directory | template               | values       | description                                           |\n| --------- | ---------------------- | ------------ | ----------------------------------------------------- |\n|  01-values | [contact.tpl](./examples/01-values/contact.tpl) | [contact.json](./examples/01-values/contact.json) | values interpolation   |\n|  01-values | [contact-with.tpl](./examples/01-values/contact-with.tpl) | [contact.json](./examples/01-values/contact.json) | values interpolation with scoped object \"with\" action  |\n|  01-values | [contact-key-index.tpl](./examples/01-values/contact-key-index.tpl) | [contact.json](./examples/01-values/contact.json) | values and keys interpolation with  \"index\" function  |\n|  01-values | [email.tpl](./examples/01-values/email.tpl) | [email.yaml](./examples/01-values/email.yaml) | values interpolation with array index  |\n|  01-values | [properties-whitespace.tpl](./examples/01-values/properties-whitespace.tpl) | [properties-whitespace.yaml](./examples/01-values/properties-whitespace.yaml) | manage whitespace in java property file  |\n|  02-loop | [db-schema.tpl](./examples/02-loop/db-schema.tpl) | [db-schema.json](./examples/02-loop/db-schema.json) | Iterate on values  |\n|  03-conditions | [logic.tpl](./examples/03-conditions/logic.tpl) | [logic.json](./examples/03-conditions/logic.json) | Logic functions  |\n|  03-conditions | [numbers.tpl](./examples/03-conditions/numbers.tpl) | [numbers.json](./examples/03-conditions/numbers.json) | Logic operators  |\n|  04-builtin-functions | [logs.tpl](./examples/04-builtin-functions/logs.tpl) | [logs.json](./examples/04-builtin-functions/logs.json) | Index and length of a list  |\n|  04-builtin-functions | [print.tpl](./examples/04-builtin-functions/print.tpl) | [print.json](./examples/04-builtin-functions/print.json) | Formatted print function  |\n|  04-builtin-functions | [escape.tpl](./examples/04-builtin-functions/escape.tpl) | [escape.json](./examples/04-builtin-functions/escape.json) | URL query string, html and javascript escape  |\n|  05-sprig-functions | N/A | N/A | [http://masterminds.github.io/sprig/](http://masterminds.github.io/sprig/) |\n|  06-custom-functions | [javabean.tpl](./examples/06-custom-functions/javabean.tpl) | [javabean.json](./examples/06-custom-functions/javabean.json) | Use of custom functions (defined in my-funcs.go)  |\n|  07-multiplefiles | [mailing.tpl](./examples/07-multiplefiles/mailing.tpl) | [mailing.json](./examples/07-multiplefiles/mailing.json) | Generate multiple files with one template an one value file  |\n|  08-subtemplate | [template.tpl](./examples/08-subtemplate/template.tpl) | [template.json](./examples/08-subtemplate/template.json) | Call a named sub-template  |\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphcollignon%2FGo-Template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphcollignon%2FGo-Template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphcollignon%2FGo-Template/lists"}