{"id":41913507,"url":"https://github.com/xybor-x/enum","last_synced_at":"2026-01-25T16:15:54.236Z","repository":{"id":266843214,"uuid":"899516526","full_name":"xybor-x/enum","owner":"xybor-x","description":"Elegant and powerful Go enums with zero code generation","archived":false,"fork":false,"pushed_at":"2025-01-02T15:00:54.000Z","size":305,"stargazers_count":41,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-11-15T14:12:10.107Z","etag":null,"topics":["enum","go","go-enum","golang-library","serialization"],"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/xybor-x.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["xybor-x","huykingsofm"]}},"created_at":"2024-12-06T12:33:56.000Z","updated_at":"2025-10-24T20:55:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"c7c84455-fe3f-4ae5-a409-eb2e0010cd3a","html_url":"https://github.com/xybor-x/enum","commit_stats":null,"previous_names":["xybor-x/enum"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/xybor-x/enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xybor-x%2Fenum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xybor-x%2Fenum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xybor-x%2Fenum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xybor-x%2Fenum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xybor-x","download_url":"https://codeload.github.com/xybor-x/enum/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xybor-x%2Fenum/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28755125,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-25T13:59:49.818Z","status":"ssl_error","status_checked_at":"2026-01-25T13:59:33.728Z","response_time":113,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["enum","go","go-enum","golang-library","serialization"],"created_at":"2026-01-25T16:15:54.086Z","updated_at":"2026-01-25T16:15:54.229Z","avatar_url":"https://github.com/xybor-x.png","language":"Go","readme":"[![Go Reference](https://pkg.go.dev/badge/github.com/xybor-x/enum.svg)](https://pkg.go.dev/github.com/xybor-x/enum)\n[![GitHub top language](https://img.shields.io/github/languages/top/xybor-x/enum?color=lightblue)](https://go.dev/)\n[![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/xybor-x/enum)](https://go.dev/blog/go1.21)\n[![GitHub release (release name instead of tag name)](https://img.shields.io/github/v/release/xybor-x/enum?include_prereleases)](https://github.com/xybor-x/enum/releases/latest)\n[![GitHub Repo stars](https://img.shields.io/github/stars/xybor-x/enum?color=blue)](https://github.com/xybor-x/enum)\n\n![Golang](./.github/go-enum.png)\n\n# ⚙️ Go Enum\n\n**Elegant, powerful, and dependency-free enums for Go with zero code generation!**\n\n\u003e [!TIP]\n\u003e This is just a ⚡ quick tutorial for general use cases.\n\u003e See more advanced features at the [documentation](./docs.md).\n\n## 🔧 Installation\n\n```sh\ngo get -u github.com/xybor-x/enum\n```\n\n## ⚡ Quick start\n\n### Define enums\n\n```go\npackage main\n\nimport \"github.com/xybor-x/enum\"\n\ntype role any\ntype Role = enum.WrapEnum[role]\n\nconst (\n    RoleUser Role = iota\n    RoleAdmin\n)\n\nfunc init() {\n    enum.Map(RoleUser, \"user\")\n    enum.Map(RoleAdmin, \"admin\")\n    enum.Finalize[Role]()\n}\n```\n\n\u003e [!CAUTION]\n\u003e Enum definitions are not thread-safe.\n\u003e Therefore, they should be finalized during initialization (at the global scope).\n\n### Usage\n\n```go\npackage main\n\nimport (\n    \"encoding/json\"\n    \"fmt\"\n)\n\ntype User struct {\n    Role Role `json:\"role\"`\n}\n\nfunc main() {\n    // Print out the string representation of enum.\n    fmt.Println(RoleAdmin) // Output: admin\n\n    // Serialize a user to json.\n    user := User{Role: RoleUser}\n    data, _ := json.Marshal(user) \n    fmt.Println(string(data)) // Output: {\"role\": \"user\"}\n}\n```\n\n### Nullable fields\n\n```go\npackage main\n\nimport (\n    \"encoding/json\"\n    \"fmt\"\n)\n\n// NullRole is similar to sql.NullXXX, designed to handle nullable SQL and JSON fields.\ntype NullRole = enum.Nullable[Role]\n\ntype User struct {\n    Role NullRole `json:\"role\"`\n}\n\nfunc main() {\n    // Serialize a nullable role with a non-null value.\n    user := User{Role: NullRole{Enum: RoleUser, Valid: true}}\n    data, _ := json.Marshal(user) \n    fmt.Println(string(data)) // Output: {\"role\": \"user\"}\n\n    // Serialize a nullable role with a null value.\n    data, _ = json.Marshal(User{})\n    fmt.Println(string(data)) // Output: {\"role\": null}\n}\n```\n\n### Integrate with protobuf\n\n*Refer to the [Integration Guide](./docs.md#-integrate-with-other-enum-systems) for details.*\n\nSuppose we have a protobuf enum defined as follows:\n\n```go\n// Code generated by protoc-gen-go.\npackage proto\n\ntype Role int32\n\nconst (\n\tRole_User  Role = 0\n\tRole_Admin Role = 1\n)\n\n...\n```\n\nWe can integrate them into `xybor-x/enum`. Here's an example:\n\n```go\npackage main\n\nimport (\n    \"path/to/proto\"\n    \"github.com/xybor-x/enum\"\n)\n\ntype Role = enum.WrapEnum[proto.Role]\n\nconst (\n    RoleUser Role = iota\n    RoleAdmin\n)\n\nfunc init() {\n    // Map the enum to protobuf enum value.\n    enum.Map(RoleUser, proto.Role_User)    \n    enum.Map(RoleAdmin, proto.Role_Admin)\n    enum.Finalize[Role]()\n}\n\nfunc main() {\n    // Convert from the protobuf enum to the Role enum.\n    role, ok := enum.From[Role](proto.Role_User)\n    // ok == true \u0026\u0026 role == RoleUser\n\n    // Convert from the Role enum to the protobuf enum.\n    role = RoleAdmin.To()\n    // role == proto.Role_Admin\n\n    // The string representation of these enums is inherited from proto.Role.\n    fmt.Println(RoleUser) // Output: User\n}\n```\n\n## 📈 Performance\n\nWhile it's true that the `xybor-x/enum` approach will generally be slower than the code generation approaches, I still want to highlight the difference.\n\nThe benchmark results are based on defining an enum with 10 values at [bench](./bench).\n\n|                   | Code generation | `xybor-x/enum` |\n| ----------------- | --------------: | -------------: |\n| ToString          |            6 ns |          17 ns |\n| FromString        |           15 ns |          22 ns |\n| json.Marshal      |          113 ns |         148 ns |\n| json.Unmarshal    |          147 ns |         144 ns |\n| SQL Value         |           29 ns |          38 ns |\n| SQL Scan (bytes)  |           29 ns |          41 ns |\n| SQL Scan (string) |           15 ns |          22 ns |\n","funding_links":["https://github.com/sponsors/xybor-x","https://github.com/sponsors/huykingsofm"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxybor-x%2Fenum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxybor-x%2Fenum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxybor-x%2Fenum/lists"}