{"id":19865765,"url":"https://github.com/BooleanCat/option","last_synced_at":"2025-05-02T05:32:01.668Z","repository":{"id":240064628,"uuid":"801571568","full_name":"BooleanCat/option","owner":"BooleanCat","description":"Support user-friendly, type-safe optionals in Go.","archived":false,"fork":false,"pushed_at":"2024-05-16T14:39:26.000Z","size":12,"stargazers_count":56,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-12T15:30:47.685Z","etag":null,"topics":[],"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/BooleanCat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-16T13:44:05.000Z","updated_at":"2024-08-30T19:43:57.000Z","dependencies_parsed_at":"2024-05-16T15:11:22.152Z","dependency_job_id":null,"html_url":"https://github.com/BooleanCat/option","commit_stats":null,"previous_names":["booleancat/option"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BooleanCat%2Foption","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BooleanCat%2Foption/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BooleanCat%2Foption/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BooleanCat%2Foption/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BooleanCat","download_url":"https://codeload.github.com/BooleanCat/option/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251992988,"owners_count":21677022,"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-11-12T15:24:07.993Z","updated_at":"2025-05-02T05:31:56.658Z","avatar_url":"https://github.com/BooleanCat.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Optional Values in Go\n\n[![GitHub release (with filter)](https://img.shields.io/github/v/release/BooleanCat/option?sort=semver\u0026logo=Go\u0026color=%23007D9C)](https://github.com/BooleanCat/option/releases) [![Actions Status](https://github.com/BooleanCat/option/workflows/test/badge.svg)](https://github.com/BooleanCat/option/actions) [![Go Reference](https://pkg.go.dev/badge/github.com/BooleanCat/option.svg)](https://pkg.go.dev/github.com/BooleanCat/option) [![Go Report Card](https://goreportcard.com/badge/github.com/BooleanCat/option)](https://goreportcard.com/report/github.com/BooleanCat/option) [![codecov](https://codecov.io/gh/BooleanCat/option/branch/main/graph/badge.svg?token=N2E43RSR14)](https://codecov.io/gh/BooleanCat/option)\n\nSupport user-friendly, type-safe optionals in Go.\n\n```go\nvalue = option.Some(4)\nno_value = option.None[int]()\n```\n\n_[Read the docs.](https://pkg.go.dev/github.com/BooleanCat/option)_\n\n## Usage\n\nThis package adds a single type, the `Option`. `Option`'s are instantiated as\none of two variants. `Some` denotes the presence of a value and `None` denotes\nthe absence.\n\nHistorically pointers have been used to denote optional values, this package\nremoves the risk of null pointer exceptions by leveraging generics to implement\ntype-safe optional values.\n\n`Options` can be tested for the presence of a value:\n\n```go\ntwo = option.Some(2)\nif two.IsSome() {\n    ...\n}\n```\n\nValues can be extracted along with a boolean test for their presence:\n\n```go\ntwo = option.Some(2)\nif value, ok := two.Value(); ok {\n    ...\n}\n```\n\nOptionals that you're sure have a value can be \"unwrapped\":\n\n```go\ntwo := option.Some(2)\ntwo.Unwrap()  // returns 2\n```\n\nAccessing a value on a `None` variant will cause a runtime panic.\n\n```go\nnone := option.None[int]()\nnone.Unwrap()  // panics\n```\n\n## Ergonomics\n\nUse of a package like this may be pervasive if you really commit to it. This\npackage was inspired by Rust's options implemenation. It might be worth\nconsidering dropping the repetative `option.` preceding the variants. Since\nimporting names into the global namespace is to be avoided, the following\nimport pattern may work for you:\n\n```go\nimport (\n    \"fmt\"\n\n    \"github.com/BooleanCat/option\"\n)\n\nvar (\n    Some = option.Some\n    None = option.None\n)\n\nfunc main() {\n    two := Some(2)\n    fmt.Println(two)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBooleanCat%2Foption","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBooleanCat%2Foption","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBooleanCat%2Foption/lists"}