{"id":34808280,"url":"https://github.com/elgopher/ptr","last_synced_at":"2026-05-25T09:34:15.445Z","repository":{"id":84732907,"uuid":"606396591","full_name":"elgopher/ptr","owner":"elgopher","description":"Generic Go functions to get optional values","archived":false,"fork":false,"pushed_at":"2023-02-26T10:36:37.000Z","size":14,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-04-25T19:32:32.444Z","etag":null,"topics":["generic","generics","go","golang","option","optional"],"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/elgopher.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":"2023-02-25T11:14:35.000Z","updated_at":"2024-06-13T12:58:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"42d329f6-c953-48d3-8099-2b01ad5bbd12","html_url":"https://github.com/elgopher/ptr","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/elgopher/ptr","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgopher%2Fptr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgopher%2Fptr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgopher%2Fptr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgopher%2Fptr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elgopher","download_url":"https://codeload.github.com/elgopher/ptr/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elgopher%2Fptr/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33469412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-25T06:32:55.349Z","status":"ssl_error","status_checked_at":"2026-05-25T06:32:35.322Z","response_time":57,"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":["generic","generics","go","golang","option","optional"],"created_at":"2025-12-25T12:11:18.768Z","updated_at":"2026-05-25T09:34:15.440Z","avatar_url":"https://github.com/elgopher.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ptr\nGeneric functions to get optional values\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/elgopher/ptr.svg)](https://pkg.go.dev/github.com/elgopher/ptr)\n[![codecov](https://codecov.io/gh/elgopher/ptr/branch/master/graph/badge.svg)](https://codecov.io/gh/elgopher/ptr)\n[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)\n\n## Problems with Go optional values\n\nBy convention, optional values in Go are pointers:\n\n```go \ntype Input struct {\n\tRequiredField string\n\tOptionalField *string\n}\n```\n\nBut such structs **cannot** be initialized in a single expression:\n\n```go\nin := Input{\n\tRequiredField: \"works\",\n\tOptionalField: \u0026(\"does not work\"),\n}\n```\n\nAnd accessing optional fields makes code look ugly:\n\n```go\nif in.OptionalField != nil \u0026\u0026 *in.OptionalField == \"value\" {\n    ...    \t\n}\n```\n\nSometimes even unsafe:\n\n```go\nvalue := \"v1\"\nin.OptionalField = \u0026value\nvalue = \"v2\" // ups... in.OptionalField is changed too! \n```\n\n\n## This tiny packages simplifies the use of optional values\n\nOne-line initialization:\n\n```go\nimport \"github.com/elgopher/ptr\"\n\nin := Input{\n\tRequiredField: \"works\",\n\tOptionalField: ptr.To(\"this also works\"),\n}\n```\n\nGetting values without boilerplate code:\n\n```go\nif ptr.Value(in.OptionalField) == \"value\" {\n\t// if in.OptionalField is nil then zero value is returned (\"\" for string)\n    ...    \t\n}\n```\n\nor get value by specifying the default value when in.OptionalField is nil:\n\n```go\nv := ptr.ValueOrDefault(in.OptionalField, \"defaultValue\")\n```\n\nSafe code:\n\n```go\nvalue := \"v1\"\nin.OptionalField = ptr.To(value)\nvalue = \"v2\" // in.OptionalField is not changed\n```\n\nor\n\n```go\nnewPointer := ptr.Copy(in.OptionalField)\n```\n\n## Installation\n\n```shell\ngo get github.com/elgopher/ptr@latest\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felgopher%2Fptr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felgopher%2Fptr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felgopher%2Fptr/lists"}