{"id":23172019,"url":"https://github.com/choonkeat/sumtype-go","last_synced_at":"2025-07-12T07:02:43.559Z","repository":{"id":209651459,"uuid":"724592380","full_name":"choonkeat/sumtype-go","owner":"choonkeat","description":"Fastest and simplest pattern matching sum types in Go. Don't be jealous of Rust anymore.","archived":false,"fork":false,"pushed_at":"2024-09-26T09:00:49.000Z","size":38,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-06T18:06:27.591Z","etag":null,"topics":["algebraic-data-types","codegenerator","golang","result-type","sumtypes","tagged-unions","union-types"],"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/choonkeat.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,"zenodo":null}},"created_at":"2023-11-28T11:55:32.000Z","updated_at":"2025-06-05T22:59:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"93fe9a9e-4255-43fd-80e4-9385e3407318","html_url":"https://github.com/choonkeat/sumtype-go","commit_stats":null,"previous_names":["choonkeat/sumtype-go"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/choonkeat/sumtype-go","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choonkeat%2Fsumtype-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choonkeat%2Fsumtype-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choonkeat%2Fsumtype-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choonkeat%2Fsumtype-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/choonkeat","download_url":"https://codeload.github.com/choonkeat/sumtype-go/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/choonkeat%2Fsumtype-go/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264951976,"owners_count":23687993,"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":["algebraic-data-types","codegenerator","golang","result-type","sumtypes","tagged-unions","union-types"],"created_at":"2024-12-18T04:20:43.577Z","updated_at":"2025-07-12T07:02:43.538Z","avatar_url":"https://github.com/choonkeat.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sumtype-go\n\n## Introduction\n\n`sumtype-go` is a CLI tool designed to facilitate the creation and management of sum types (aka union types) in Go. This tool simplifies the process of generating boilerplate code for discriminated union types, making it easier to work with its variants in Go.\n\n## Quick Tour\n\nTo define this sum type:\n\n```elm\ntype User\n    = Anonymous\n    | Member String Time\n    | Admin String\n```\n\nwrite this Go type definition, e.g. in `declaration.go`\n\n```go\ntype UserVariants struct { // be sure to suffix the name with `Variants`\n\tAnonymous func()\n\tMember    func(email string, since time.Time)\n\tAdmin     func(email string)\n}\n```\n\nExecute this command to generate `User` type from your `UserVariants` struct\n\n```sh\ngo install github.com/choonkeat/sumtype-go@v0.4.1 # to install\nsumtype-go -input declaration.go\n```\n\nTo generate `declaration.boilerplate.go` and start using `User`!\n\n```go\nusers := []User{\n\tAnonymous(),                 // this returns a `User` value\n\tMember(\"Alice\", time.Now()), // this also returns a `User` value\n\tAdmin(\"Bob\"),                // this also returns a `User` value\n}\n```\n\nand we can pattern match `User` values and return a different value depending on pattern matched variant\n\n```go\nuserString := UserMap(user, UserVariantsMap[string]{\n\tAnonymous: func() string {\n\t\treturn \"Anonymous coward\"\n\t},\n\tMember: func(email string, since time.Time) string {\n\t\treturn email + \" (member since \" + since.String() + \")\"\n\t},\n\tAdmin: func(email string) string {\n\t\treturn email + \" (admin)\"\n\t},\n})\n```\n\nor _do_ different things depending on pattern matched variant\n\n```go\nfunc SendReply(u User, comment Comment) {\n\tu.Match(UserVariants{\n\t\tAnonymous: func() {\n\t\t\t// noop\n\t\t},\n\t\tMember: func(email string, since time.Time) {\n\t\t\tsendEmail(email, comment)\n\t\t},\n\t\tAdmin: func(email string) {\n\t\t\tsendEmail(email, comment)\n\t\t},\n\t})\n}\n```\n\nRefer to `example/gosumtype_1_*.go`\n\n## Generics\n\nWe support generics too. e.g. the classic `Result` type\n\n```elm\ntype Result x a\n    = Err x\n    | Ok a\n```\n\ncan be defined as\n\n```go\ntype ResultVariants[x, a any] struct {\n\tErr func(err x)\n\tOk  func(data a)\n}\n```\n\nSame thing, after executing `sumtype-go` to generate the `.boilerplate.go` file, you can use\nthe generated `Result` like this\n\n```go\nresults := []Result[string, int]{\n\tErr[string, int](\"Oops err\"), // this returns a `Result` value\n\tOk[string, int](42),          // this also returns a `Result` value\n}\n\nfor i, result := range results {\n\tHandleResult(i, result) // implement your own `func HandleResult(int, Result[string, int])`\n}\n```\n\nRefer to `example/result_1_*.go`\n\n## Installation\n\nTo install `sumtype-go`, ensure you have Go installed on your system, and then run the following command:\n\n```sh\ngo install github.com/choonkeat/sumtype-go@v0.4.1\n```\n\n## Usage\n\nAfter installation, you can start using `sumtype-go` by invoking it from the command line.\n\n```\n$ sumtype-go -h\nUsage of sumtype-go:\n  -input string\n    \tInput file name\n  -pattern-match string\n    \tName of the pattern match method (default \"Match\")\n  -suffix string\n    \tSuffix of the struct defining variants (default \"Variants\")\n```\n\nHere's a basic example of how to use it:\n\n```\nsumtype-go -input example/gosumtype_1_declaration.go\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoonkeat%2Fsumtype-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchoonkeat%2Fsumtype-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchoonkeat%2Fsumtype-go/lists"}