{"id":44562226,"url":"https://github.com/dgrr/gtl","last_synced_at":"2026-02-13T23:43:24.026Z","repository":{"id":57580491,"uuid":"361825117","full_name":"dgrr/gtl","owner":"dgrr","description":"Golang Template Library (GTL). Common data structures using Golang generics.","archived":false,"fork":false,"pushed_at":"2022-09-28T12:19:36.000Z","size":72,"stargazers_count":5,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-11-23T02:21:27.187Z","etag":null,"topics":["generics","go","go2","golang","vector"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dgrr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-04-26T16:48:48.000Z","updated_at":"2025-10-23T03:43:45.000Z","dependencies_parsed_at":"2022-09-14T17:22:18.787Z","dependency_job_id":null,"html_url":"https://github.com/dgrr/gtl","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/dgrr/gtl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrr%2Fgtl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrr%2Fgtl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrr%2Fgtl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrr%2Fgtl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dgrr","download_url":"https://codeload.github.com/dgrr/gtl/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dgrr%2Fgtl/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29423542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T22:20:51.549Z","status":"ssl_error","status_checked_at":"2026-02-13T22:20:49.838Z","response_time":78,"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":["generics","go","go2","golang","vector"],"created_at":"2026-02-13T23:43:20.543Z","updated_at":"2026-02-13T23:43:24.011Z","avatar_url":"https://github.com/dgrr.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DEPRECATION!\n\n## Use TL (https://github.com/dgrr/tl)\n\n# GTL: Golang Template Library (WIP)\n\nGTL is a template library written in pure Go(2).\nIt is intended to hold common data structures that might be missing in the standard library\nor they are cumbersome if generics are not present (using interface{} as replacement, type casting, etc...).\n\nYou can learn more about Golang's generics [here](https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md).\n\n# Table of Contents\n1. [Result](#result)\n2. [Optional](#optional)\n3. [Iterator](#iterator)\n4. [Vector](#vector)\n5. [Bytes](#bytes)\n6. [Numeric](#numeric)\n7. [Pair](#pair)\n\n## Result\n\nResult tries to emulate [Rust's result](https://doc.rust-lang.org/std/result/).\nBut being honest, it is more similar to [std::expected](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0323r3.pdf) from C++.\n\nAs Golang already has a type for defining errors, the second type is replaced with the error.\n\nAfter getting the returned value,\nwe can manage the [Result](https://github.com/dgrr/gtl/blob/master/result.go2#L4) in different ways.\n\nA valid usage would be:\n```go\nfunc Do() (r gtl.Result[string]) {\n  if rand.Intn(100) \u0026 1 == 0 { // is even?\n    return r.Err(\n      errors.New(\"unexpected result\"))\n  }\n  \n  return r.Ok(\"Success\")\n}\n\nfunc onSuccess(v string) {\n\tfmt.Printf(\"Got result: %s\\n\", v)\n}\n\nfunc onError(err error) {\n\tfmt.Printf(\"Got error: %s\\n\", err)\n}\n\nfunc main() {\n  Do().Then(onSuccess).Else(onError)\n}\n```\n\n`Then` is executed if `Do` returned the call to `Ok`. `Ok` will store the string `\"Success\"`\ninto the Result's expected value. In the other hand, `Else` will be executed if `Err` has been called.\n\nIf we don't want to handle errors but we just want to get the value, we can do the following:\n```go\nfunc Do() (r gtl.Result[string]) {\n  // assume Do() didn't change\n}\n\nfunc main() {\n  fmt.Printf(\"Got %s\\n\", Do().Or(\"Failed\"))\n}\n```\n\n## Optional\n\nOptional represents an optional value. In C++ we have the [std::optional](https://en.cppreference.com/w/cpp/utility/optional)\nwhich might be similar.\n\nA valid usage would be:\n```go\nfunc myFunc() (o gtl.Optional[int]) {\n\tif n := rand.Int(); n % 2 == 0 { // is even\n\t\to.Set(n)\n\t}\n\t\n\treturn o\n}\n\nfunc main() {\n\tvalue := myFunc()\n\tif value.Has() {\n\t\tfmt.Printf(\"Got: %d\\n\", value.V())\n\t}\n}\n```\n\n## Iterator\n\nIterator tries to emulate a [C++'s iterator](https://en.cppreference.com/w/cpp/iterator/iterator).\nIt is defined as follows:\n```go\n// Iterator defines an interface for iterative objects.\ntype Iterator[T any] interface {\n\t// Next increments the iterator.\n\tNext() bool\n\t// Advance advances the cursor `n` steps. Returns false if `n` overflows.\n\tAdvance(n int) bool\n\t// Get returns the value held in the iterator.\n\tGet() T\n\t// Ptr returns a pointer to T.\n\tPtr() *T\n}\n```\n\n## Vector\n\nVec tries to emulate a [C++'s vector](https://en.cppreference.com/w/cpp/container/vector) (somehow).\nIt doesn't try to emulate it exactly, but it just works as a C++ vector in a way that internally is just\na slice with some helper functions, in this case functions like `Append`, `Push`, `PopBack`, `PopFront` or `Len`.\n\nA valid usage would be:\n```go\npackage main\n\nimport (\n        \"os\"\n        \"sort\"\n        \"fmt\"\n\n        \"github.com/dgrr/gtl\"\n)\n\nfunc main() {\n        vec := gtl.NewVec(os.Args[1:]...)\n\n        sort.Slice(vec, func(i, j int) bool {\n                return vec[i] \u003c vec[j]\n        })\n\n        fmt.Println(vec)\n}\n```\n\n## Bytes\n\nBytes is a helper for working with byte slices. It can be used as\na normal slice for reading data from a net.Conn.\n\nYou can see an example of how to use Bytes [here](https://github.com/dgrr/gtl/blob/2642e2ac98bd8a8fbfbc3e9789d4b87bf6e6e317/examples/echo_tcp/main.go2#L73).\n\n### Numeric\n\nThere are global numeric helper functions like [Max](https://github.com/dgrr/gtl/blob/b5b6ba36de904e757d00f78351c577a6ad0547e1/numeric.go2#L9)\nand [Min](https://github.com/dgrr/gtl/blob/b5b6ba36de904e757d00f78351c577a6ad0547e1/numeric.go2#L19).\n\n### Pair\n\nPair is a data structure that holds 2 values [T, U].\n```\npair := gtl.MakePair(\"hello\", 1234)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrr%2Fgtl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdgrr%2Fgtl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdgrr%2Fgtl/lists"}