{"id":13786653,"url":"https://github.com/alexpantyukhin/go-pattern-match","last_synced_at":"2025-04-07T06:07:42.774Z","repository":{"id":38420630,"uuid":"161388009","full_name":"alexpantyukhin/go-pattern-match","owner":"alexpantyukhin","description":"Pattern matchings for Go.","archived":false,"fork":false,"pushed_at":"2023-03-01T21:02:48.000Z","size":2043,"stargazers_count":246,"open_issues_count":2,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-31T04:07:30.990Z","etag":null,"topics":["awesome-go","awesome-golang","go","golang","golang-library","pattern-matching"],"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/alexpantyukhin.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":"2018-12-11T20:11:17.000Z","updated_at":"2025-03-18T02:45:36.000Z","dependencies_parsed_at":"2024-06-18T15:49:09.391Z","dependency_job_id":"73eb2ae2-bd6f-4528-8cfb-b2d179e68aaa","html_url":"https://github.com/alexpantyukhin/go-pattern-match","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/alexpantyukhin%2Fgo-pattern-match","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpantyukhin%2Fgo-pattern-match/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpantyukhin%2Fgo-pattern-match/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexpantyukhin%2Fgo-pattern-match/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexpantyukhin","download_url":"https://codeload.github.com/alexpantyukhin/go-pattern-match/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601448,"owners_count":20964864,"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":["awesome-go","awesome-golang","go","golang","golang-library","pattern-matching"],"created_at":"2024-08-03T19:01:27.199Z","updated_at":"2025-04-07T06:07:42.754Z","avatar_url":"https://github.com/alexpantyukhin.png","language":"Go","funding_links":[],"categories":["Utilities","工具库","公用事业公司","工具库`可以提升效率的通用代码库和工具`","Utility"],"sub_categories":["Utility/Miscellaneous","Fail injection","HTTP Clients","查询语","实用程序/Miscellaneous"],"readme":"# Go pattern matching\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n[![Go Report Card](https://goreportcard.com/badge/github.com/alexpantyukhin/go-pattern-match)](https://goreportcard.com/report/github.com/alexpantyukhin/go-pattern-match)\n[![build](https://github.com/alexpantyukhin/go-pattern-match/actions/workflows/build.yml/badge.svg)](https://github.com/alexpantyukhin/go-pattern-match/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/alexpantyukhin/go-pattern-match/branch/master/graph/badge.svg)](https://codecov.io/gh/alexpantyukhin/go-pattern-match)\n[![GoDoc](https://godoc.org/alexpantyukhin/go-pattern-match?status.svg)](https://godoc.org/github.com/alexpantyukhin/go-pattern-match)\n[![LICENSE](https://img.shields.io/github/license/alexpantyukhin/go-pattern-match.svg)](https://github.com/alexpantyukhin/go-pattern-match/blob/master/LICENSE)\n\nIt's just another implementation of pattern matching in Go. I have been inspired by [Python pattern matching](https://github.com/santinic/pampy), that's why I wanted to try writing something similar in Go :)\nFor now the following matching are implemented :\n   - [x] Simple types (like int, int64, float, float64, bool..).\n   - [x] Struct type.\n   - [x] Slices (with HEAD, TAIL, OneOf patterns).\n   - [x] Dictionary (with ANY, OneOf pattern).\n   - [x] Regexp.\n   - [x] Additional custom matching (ability to add special matching for some, structs for example).\n   \n# Usages\n\n## Fibonacci example:\n\n```go\nfunc fib(n int) int {\n\t_, res := match.Match(n).\n\t\tWhen(1, 1).\n\t\tWhen(2, 1).\n\t\tWhen(match.ANY, func() int { return fib(n-1) + fib(n-2) }).\n\t\tResult()\n\n\treturn res.(int)\n}\n```\n\n## Simple types:\n\n```go\nisMatched, mr := match.Match(42).\n                When(42, 10).\n                Result()\n// isMatched - true, mr - 10\n```\n\n## With Structs:\n- Simple check value by type\n```go\nval := TestStruct{1}\n\nisMatched, _ := Match(val).\n    When(func(TestStruct) {},  1).\n    Result()\n```\n\n- Check value by type and condition\n```go\nval := TestStruct{1}\n\nisMatched, _ := Match(val).\n\tWhen(func(ts TestStruct) bool { return ts.value == 42 }, 1).\n\tWhen(func(ts AnotherStruct) bool { return ts.stringValue == \"hello\" }, 2).\n\tResult()\n```\n\n## With Maps:\n```go\nisMatched, mr := match.Match(map[string]int{\n                \t\"rsc\": 3711,\n                \t\"r\":   2138,\n                \t\"gri\": 1908,\n                \t\"adg\": 912,\n                }).\n        \t    When(map[string]interface{}{\n                \t\"rsc\": 3711,\n                \t\"r\":   2138,\n                \t\"gri\": 1908,\n                \t\"adg\": match.ANY,\n            \t}, true).\n            \tResult()\n```\n\n## With Slices:\n```go\nisMatched, mr := match.Match([]int{1, 2, 3, 4, 5, 6}).\n            \tWhen([]interface{}{match.HEAD, 3, match.OneOf(3, 4), 5, 6}, 125).\n            \tResult()\n```\n\n## With regexps:\n```go\nisMatched, mr := match.Match(\"gophergopher\").\n            \tWhen(\"gophergopher\", func() interface{} { return true }).\n            \tResult()\n```\n\n## Without result:\n```go\nfunc main() {\n\tMatch(val).\n\tWhen(42, func() { fmt.Println(\"You found the answer to life, universe and everything!\") }).\n\tWhen(ANY, func() { fmt.Println(\"No.. It's not an answer.\") }).\n\tResult()\n}\n```\n\n# Installation\nJust `go get` this repository in the following way:\n\n```\ngo get github.com/alexpantyukhin/go-pattern-match\n```\n\n# Full example\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/alexpantyukhin/go-pattern-match\"\n)\n\nfunc main() {\n    isMatched, mr := match.Match([]int{1, 2, 3}).\n        When(42, false).\n        When([]interface{}{match.HEAD, 2, 3}, true).\n        Result()\n\n\n    if isMatched {\n        fmt.Println(mr)\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpantyukhin%2Fgo-pattern-match","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexpantyukhin%2Fgo-pattern-match","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexpantyukhin%2Fgo-pattern-match/lists"}