{"id":18403753,"url":"https://github.com/russellluo/gopt","last_synced_at":"2025-04-07T07:32:47.672Z","repository":{"id":162327730,"uuid":"636697276","full_name":"RussellLuo/gopt","owner":"RussellLuo","description":"Generic Functional Options for Go.","archived":false,"fork":false,"pushed_at":"2023-05-08T09:44:52.000Z","size":10,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-22T14:41:32.908Z","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/RussellLuo.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-05-05T12:43:00.000Z","updated_at":"2024-03-20T21:51:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"e6c9e358-c407-4e33-9e46-d5211e7d69ff","html_url":"https://github.com/RussellLuo/gopt","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/RussellLuo%2Fgopt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fgopt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fgopt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RussellLuo%2Fgopt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RussellLuo","download_url":"https://codeload.github.com/RussellLuo/gopt/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247612525,"owners_count":20966769,"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-06T02:48:06.499Z","updated_at":"2025-04-07T07:32:47.666Z","avatar_url":"https://github.com/RussellLuo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# gopt\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/RussellLuo/gopt/vulndb.svg)][1]\n\nGeneric Functional Options for Go.\n\n\n## Installation\n\n\n```bash\n$ go get -u github.com/RussellLuo/gopt\n```\n\n\n## Quick Start\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/RussellLuo/gopt\"\n)\n\ntype Server struct {\n\thost string\n\tport int\n}\n\nfunc New(options ...gopt.Option[*Server]) *Server {\n\treturn gopt.Apply(new(Server), options...)\n}\n\n// ServerOption holds all option factories for Server.\ntype ServerOption struct{}\n\nfunc (ServerOption) Host(host string) gopt.Option[*Server] {\n\treturn func(s *Server) { s.host = host }\n}\n\nfunc (ServerOption) Port(port int) gopt.Option[*Server] {\n\treturn func(s *Server) { s.port = port }\n}\n\nfunc main() {\n\tserver := New(\n\t\tServerOption{}.Host(\"localhost\"),\n\t\tServerOption{}.Port(8080),\n\t)\n\tfmt.Printf(\"server: %+v\\n\", server)\n\n\t// Output:\n\t// server: \u0026{host:localhost port:8080}\n}\n```\n\n\n## FAQ\n\n1. Why might I want to use this tiny library?\n\n    Traditional Functional Options will define many exported functions, which is likely to cause naming conflicts.\n\n    Reference articles:\n    - [Golang Functional Options Pattern][2]\n    - Go Patterns' [Functional Options][3]\n\n    Reference projects:\n    - gRPC-Go's [DialOption][4]\n    - Go kit's [ServerOption][5]\n\n2. Why is `ServerOption{}` used in the example?\n\n    `ServerOption` is a named alias of [the empty struct][6], whose instances are fungible and consume no space.\n\n\n## Unsafe gopt\n\n[Unsafe gopt](unsafe/gopt) is an early implementation, which is deprecated and for reference only.\n\n\n## License\n\n[MIT](LICENSE)\n\n\n[1]: https://pkg.go.dev/github.com/RussellLuo/gopt\n[2]: https://golang.cafe/blog/golang-functional-options-pattern.html\n[3]: https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md\n[4]: https://github.com/grpc/grpc-go/blob/v1.55.0/dialoptions.go#L83\n[5]: https://github.com/go-kit/kit/blob/v0.12.0/transport/http/server.go#L47\n[6]: https://dave.cheney.net/2014/03/25/the-empty-struct\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frussellluo%2Fgopt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frussellluo%2Fgopt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frussellluo%2Fgopt/lists"}