{"id":18088593,"url":"https://github.com/sxyazi/go-collection","last_synced_at":"2025-04-13T02:32:53.966Z","repository":{"id":47537759,"uuid":"455524846","full_name":"sxyazi/go-collection","owner":"sxyazi","description":"Useful collection functions for golang, based on generic types.","archived":false,"fork":false,"pushed_at":"2022-07-21T00:49:40.000Z","size":144,"stargazers_count":43,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-10T06:57:36.540Z","etag":null,"topics":["collection","generic-types","go-collection","golang","golang-collection"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/sxyazi/go-collection@v0.0.3","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/sxyazi.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}},"created_at":"2022-02-04T11:36:42.000Z","updated_at":"2025-03-03T23:42:49.000Z","dependencies_parsed_at":"2022-09-10T14:11:06.894Z","dependency_job_id":null,"html_url":"https://github.com/sxyazi/go-collection","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxyazi%2Fgo-collection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxyazi%2Fgo-collection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxyazi%2Fgo-collection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxyazi%2Fgo-collection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sxyazi","download_url":"https://codeload.github.com/sxyazi/go-collection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248657826,"owners_count":21140842,"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":["collection","generic-types","go-collection","golang","golang-collection"],"created_at":"2024-10-31T17:13:51.702Z","updated_at":"2025-04-13T02:32:53.635Z","avatar_url":"https://github.com/sxyazi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-collection\n\nEnglish | [简体中文](README_zh-CN.md)\n\n`go-collection` provides developers with a convenient set of functions for working with common slices, maps, and arrays data. These functions are based on the generic types of Go 1.18, which makes it easier to use them without annoying type assertions. In addition to using these functions directly, it also supports method chaining.\n\n```go\ncollect.Reduce(collect.Filter(collect.Map([]int{1, 2, 3}, fn), fn), fn)\n```\n\nEquivalent to:\n\n```go\ncollect.UseSlice([]int{1, 2, 3}).Map(fn).Filter(fn).Reduce(fn)\n```\n\n## Installation\n\n```shell\ngo get -u github.com/sxyazi/go-collection\n```\n\nThen import it\n\n```go\nimport collect \"github.com/sxyazi/go-collection\"\n```\n\n## API\n\nIts API is very simple and if you have used other similar packages, you should be able to get started with it in a few minutes. **For convenience, they are described below in function form**.\n\n### Slice\n\nThe corresponding chained function is `collect.UseSlice()`\n\n- `Len` gets the length of the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := []int{1, 2, 3}\n  collect.Len(d1) // 3\n\n  d2 := []string{\"a\", \"b\", \"c\"}\n  collect.Len(d2) // 3\n  ```\n\n  \u003c/details\u003e\n\n- `Each` iterates over each element in the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []float64{1, 2, 3}\n  collect.Each(d, func(value float64, index int) {\n    fmt.Println(index, value)\n  })\n  ```\n\n  \u003c/details\u003e\n\n- `Empty` checks if the slice is empty\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  var d []int\n  collect.Empty(d) // true\n  ```\n\n  \u003c/details\u003e\n\n- `Same` checks if the contents of two slices are the same\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := []int{1, 2, 3}\n  d2 := []int{1, 2, 3}\n  collect.Same(d1, d2) // true\n\n  d3 := [][]int{{1, 2, 3}, {4, 5, 6}}\n  d4 := [][]int{{1, 2, 3}, {4, 5, 6}}\n  collect.Same(d3, d4) // true\n  ```\n\n  \u003c/details\u003e\n\n- `First` gets the first element of the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := []int{1, 2, 3}\n  value, ok := collect.First(d1) // 1, true\n\n  var d2 []int\n  value, ok = collect.First(d2) // 0, false\n  ```\n\n  \u003c/details\u003e\n\n- `Last` gets the last element of the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := []int{1, 2, 3}\n  value, ok := collect.Last(d1) // 3, true\n\n  var d2 []int\n  value, ok = collect.Last(d2) // 0, false\n  ```\n\n  \u003c/details\u003e\n\n- `Index` gets the index of the specified element in the slice, and returns -1 if it does not exist.\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := []int{1, 2, 3}\n  collect.Index(d1, 2) // 1\n\n  s1 := []string{\"a\", \"b\", \"c\"}\n  s2 := []string{\"d\", \"e\", \"f\"}\n  collect.Index([][]string{s1, s2}, s2) // 1\n  ```\n\n  \u003c/details\u003e\n\n- `Contains` checks if the slice contains the specified element\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := []int{1, 2, 3}\n  collect.Contains(d1, 2) // true\n\n  s1 := []string{\"a\", \"b\", \"c\"}\n  s2 := []string{\"d\", \"e\", \"f\"}\n  collect.Contains([][]string{s1, s2}, s2) // true\n  ```\n\n  \u003c/details\u003e\n\n- `Diff` computes the difference set of two slices\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2, 3}\n  collect.Diff(d, []int{2, 3})  // []int{1}\n  ```\n\n  \u003c/details\u003e\n\n- `Filter` filters the elements in the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Filter([]int{1, 2, 3, 4, 5}, func(value, index int) bool {\n    return value % 2 == 0\n  })  // []int{2, 4}\n  ```\n\n  \u003c/details\u003e\n\n- `Map` iterates over and sets the value of the elements in the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Map([]int{1, 2, 3}, func(value, index int) int {\n    return value * 2\n  })  // []int{2, 4, 6}\n  ```\n\n  \u003c/details\u003e\n\n- `Unique` removes duplicate elements in the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2, 3, 3, 4}\n  collect.Unique(d)  // []int{1, 2, 3, 4}\n  ```\n\n  \u003c/details\u003e\n\n- `Duplicates` gets the duplicate elements in the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []string{\"a\", \"b\", \"a\", \"c\"}\n  collect.Duplicates(d)  // map[int]string{2: \"a\"}\n  ```\n\n  \u003c/details\u003e\n\n- `Merge` merges the current slice with other slices\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := []int{1, 2}\n  d2 := []int{3, 4}\n  d3 := []int{5, 6}\n\n  collect.Merge(d1, d2)      // []int{1, 2, 3, 4}\n  collect.Merge(d1, d2, d3)  // []int{1, 2, 3, 4, 5, 6}\n  ```\n\n  \u003c/details\u003e\n\n- `Random` gets an element of the slice at random\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2}\n  value, ok := collect.Random(d)  // 1 or 2, true\n\n  d := []int{}\n  value, ok := collect.Random(d)  // 0, false\n  ```\n\n  \u003c/details\u003e\n\n- `Reverse` reverses the elements in a slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2}\n  collect.Reverse(d)  // []int{2, 1}\n  ```\n\n  \u003c/details\u003e\n\n- `Shuffle` randomly shuffles the elements in a slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2}\n  collect.Shuffle(d)  // []int{1, 2} or []int{2, 1}\n  ```\n\n  \u003c/details\u003e\n\n- `Slice` takes a segment from a slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  Function signature: `Slice(items T, offset int)`\n\n  ```go\n  d := []int{1, 2, 3, 4, 5}\n  collect.Slice(d, 2)   // []int{3, 4, 5}\n  collect.Slice(d, -1)  // []int{5}\n  collect.Slice(d, -2)  // []int{4, 5}\n  ```\n\n  Function signature: `Slice(items T, offset, length int)`\n\n  ```go\n  d := []int{1, 2, 3, 4, 5}\n  collect.Slice(d, 0, 2)   // []int{1, 2}\n  collect.Slice(d, 2, 3)   // []int{3, 4, 5}\n  collect.Slice(d, 3, -2)  // []int{3, 4}\n  collect.Slice(d, -4, 3)  // []int{2, 3, 4}\n  ```\n\n  \u003c/details\u003e\n\n- `Split` splits a slice into multiple slices by the specified amount\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2, 3, 4, 5}\n  collect.Split(d, 2)  // [][]int{{1, 2}, {3, 4}, {5}}\n  ```\n\n  \u003c/details\u003e\n\n- `Splice` removes a segment from the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  Function signature: `Splice(items T, offset int)`\n\n  ```go\n  d := []int{1, 2, 3, 4, 5}\n  collect.Splice(\u0026d, 2)  // []int{3, 4, 5}\n  d                      // []int{1, 2}\n  ```\n\n  Function signature: `Splice(items T, offset, length int)`\n\n  ```go\n  d := []int{1, 2, 3, 4, 5}\n  collect.Splice(\u0026d, 2, 2)  // []int{3, 4}\n  d                         // []int{1, 2, 5}\n  ```\n\n  Function signature: `Splice(items T, offset, length int, replacements ...T|E)`\n\n  ```go\n  d1 := []int{1, 2, 3, 4}\n  collect.Splice(\u0026d1, 1, 2, []int{22, 33})  // []int{2, 3}\n  d1                                        // []int{1, 22, 33, 4}\n\n  d2 := []int{1, 2, 3, 4}\n  collect.Splice(\u0026d2, 1, 2, 22, 33)  // []int{2, 3}\n  d2                                 // []int{1, 22, 33, 4}\n\n  d3 := []int{1, 2, 3, 4}\n  collect.Splice(\u0026d3, 1, 2, []int{22}, 33, []int{55})  // []int{2, 3}\n  d3                                                   // []int{1, 22, 33, 55, 4}\n  ```\n\n  It is worth noting that this method also supports the use of negative numbers as arguments, and its behavior is the same as that of `Slice`, which is not repeated here due to space constraints.\n\n  \u003c/details\u003e\n\n- `Reduce` reduces the collection to a single value, and the parameters of each iteration are the results of the previous iteration\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Reduce([]int{1, 2, 3}, 100, func(carry, value, key int) int {\n  \treturn carry + value\n  })  // 106\n  ```\n\n  \u003c/details\u003e\n\n- `Pop` removes and returns the last element of the collection\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2}\n  v, ok := collect.Pop(\u0026d)  // 2, true\n  d                         // []int{1}\n\n  c := collect.UseSlice([]int{1, 2})\n  v, ok := c.Pop()  // 2, true\n  c.All()           // []int{1}\n  ```\n\n  \u003c/details\u003e\n\n- `Push` appends an element to the end of a collection\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []int{1, 2}\n  Push(\u0026d, 3)\n  d  // []int{1, 2, 3}\n\n  collect.UseSlice([]int{1, 2}).Push(3).All()  // []int{1, 2, 3}\n  ```\n\n  \u003c/details\u003e\n\n- `Where` filters the collection by the specified rules\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  Function signature: `Where(items T, target any)`\n\n  ```go\n  collect.Where([]int{1, 2, 3}, 2)  // []int{2}\n  ```\n\n  Function signature: `Where(items T, operator string, target any)`\n\n  ```go\n  d := []int{1, 2, 3, 4}\n  collect.Where(d, \"=\", 2)   // []int{2}\n  collect.Where(d, \"!=\", 2)  // []int{1, 3, 4}\n  collect.Where(d, \"\u003e\", 2)   // []int{3, 4}\n  collect.Where(d, \"\u003e=\", 2)  // []int{2, 3, 4}\n  collect.Where(d, \"\u003c\", 3)   // []int{1, 2}\n  collect.Where(d, \"\u003c=\", 3)  // []int{1, 2, 3}\n  ```\n\n  Function signature: `Where(items T, key any, target any)`\n\n  ```go\n  d := []User{{ID: 1, Name: \"Hugo\"}, {ID: 2, Name: \"Lisa\"}, {ID: 3, Name: \"Iris\"}, {ID: 4, Name: \"Lisa\"}}\n  collect.Where(d, \"Name\", \"Lisa\")  // []User{{2 Lisa} {4 Lisa}}\n  ```\n\n  Function signature: `Where(items T, key any, operator string, target any)`\n\n  ```go\n  d := []User{{ID: 1, Name: \"Hugo\"}, {ID: 2, Name: \"Lisa\"}, {ID: 3, Name: \"Iris\"}, {ID: 4, Name: \"Lisa\"}}\n  collect.Where(d, \"Name\", \"!=\", \"Lisa\")  // []User{{1 Hugo} {3 Iris}}\n  ```\n\n  \u003c/details\u003e\n\n- `WhereIn` removes elements from the collection that do not exist in the specified slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  Function signature: `WhereIn(items T, targets []any)`\n\n  ```go\n  d := []int{1, 2, 3, 4}\n  collect.WhereIn(d, []int{2, 3})  // []int{2, 3}\n  ```\n\n  Function signature: `WhereIn(items T, key any, targets []any)`\n\n  ```go\n  d := []User{{ID: 1, Name: \"Hugo\"}, {ID: 2, Name: \"Lisa\"}, {ID: 3, Name: \"Iris\"}, {ID: 4, Name: \"Lisa\"}}\n  collect.WhereIn(d, \"Name\", []string{\"Hugo\", \"Iris\"})  // []User{{1 Hugo} {3 Iris}}\n  ```\n\n  \u003c/details\u003e\n\n- `WhereNotIn` removes elements from the collection that exist in the specified slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  Function signature: `WhereNotIn(items T, targets []any)`\n\n  ```go\n  d := []int{1, 2, 3, 4}\n  collect.WhereNotIn(d, []int{2, 3})  // []int{1, 4}\n  ```\n\n  Function signature: `WhereNotIn(items T, key any, targets []any)`\n\n  ```go\n  d := []User{{ID: 1, Name: \"Hugo\"}, {ID: 2, Name: \"Lisa\"}, {ID: 3, Name: \"Iris\"}, {ID: 4, Name: \"Lisa\"}}\n  collect.WhereNotIn(d, \"Name\", []string{\"Lisa\", \"Iris\"})  // []User{{1 Hugo}}\n  ```\n\n  \u003c/details\u003e\n\n### Array\n\nExactly the same as [slice](#Slice), you just pass in the array converted to a slice:\n\n```go\narr := [3]int{1, 2, 3}\n\ncollect.Len(arr[:])\n// or\ncollect.UseSlice(arr[:]).Len()\n```\n\n### Map\n\nThe corresponding chained function is `collect.UseMap()`\n\n- `Len` gets the number of elements in the map\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := map[string]int{\"a\": 1, \"b\": 2, \"c\": 3}\n  collect.Len(d1) // 3\n  ```\n\n  \u003c/details\u003e\n\n- `Empty` checks if the map is empty\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  var d map[string]int\n  collect.Empty(d) // true\n  ```\n\n  \u003c/details\u003e\n\n- `Only` gets the elements of the map with the specified keys\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := map[string]int{\"a\": 1, \"b\": 2, \"c\": 3}\n  collect.Only(d, \"a\")       // map[string]int{\"a\": 1}\n  collect.Only(d, \"a\", \"b\")  // map[string]int{\"a\": 1, \"b\": 2}\n  ```\n\n  \u003c/details\u003e\n\n- `Except` gets the elements of the map with the specified keys removed\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := map[string]int{\"a\": 1, \"b\": 2, \"c\": 3}\n  collect.Except(d, \"a\")       // map[string]int{\"b\": 2, \"c\": 3}\n  collect.Except(d, \"a\", \"b\")  // map[string]int{\"c\": 3}\n  ```\n\n  \u003c/details\u003e\n\n- `Keys` gets all the keys in the map\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := map[string]int{\"a\": 1, \"b\": 2, \"c\": 3}\n  collect.Keys(d)  // []string{\"a\", \"b\", \"c\"}\n  ```\n\n  \u003c/details\u003e\n\n- `DiffKeys` compares with the given collection and returns the key/value pairs in the given collection that do not exist in the original collection\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := map[string]int{\"a\": 1, \"b\": 2, \"c\": 3}\n  d2 := map[string]int{\"b\": 22, \"c\": 33}\n\n  collect.DiffKeys(d1, d2)  // map[string]int{\"a\": 1}\n  ```\n\n  \u003c/details\u003e\n\n- `Has` checks if the map contains the specified key\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := map[string]int{\"a\": 1}\n  collect.Has(d, \"a\")  // true\n  ```\n\n  \u003c/details\u003e\n\n- `Get` gets the value of the specified key in the map\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := map[string]int{\"a\": 1}\n\n  value, ok := collect.Get(d, \"a\")  // 1, true\n  value, ok := collect.Get(d, \"b\")  // 0, false\n  ```\n\n  \u003c/details\u003e\n\n- `Put` sets the value of the specified key in the map\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := map[string]int{\"a\": 1}\n  collect.Put(d, \"b\", 2)  // map[string]int{\"a\": 1, \"b\": 2}\n  ```\n\n  \u003c/details\u003e\n\n- `Pull` removes the specified key from the collection and returns its value\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := map[string]int{\"a\": 1, \"b\": 2}\n  v, ok := collect.Pull(d, \"b\")  // 2, true\n  d                              // map[string]int{\"a\": 1}\n  ```\n\n  \u003c/details\u003e\n\n- `Merge` merges the current map with other maps\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := map[string]int{\"a\": 1, \"b\": 2}\n  d2 := map[string]int{\"b\": 22}\n  d3 := map[string]int{\"b\": 222, \"c\": 3}\n\n  collect.MapMerge(d1, d2)            // map[string]int{\"a\": 1, \"b\": 22}\n  collect.UseMap(d1).Merge(d2).All()  // Equal to the above\n\n  collect.MapMerge(d1, d2, d3)            // map[string]int{\"a\": 1, \"b\": 222, \"c\": 3}\n  collect.UseMap(d1).Merge(d2, d3).All()  // Equal to the above\n  ```\n\n  \u003c/details\u003e\n\n- `Union` unites the current map with other maps, and the items in the original map are given priority\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d1 := map[string]int{\"a\": 1, \"b\": 2}\n  d2 := map[string]int{\"b\": 22, \"c\": 3}\n  collect.Union(d1, d2)  // map[string]int{\"a\": 1, \"b\": 2, \"c\": 3}\n  ```\n\n  \u003c/details\u003e\n\n### Number slice\n\nThe corresponding chained function is `collect.UseNumber()`，which is a subset of [slice](#Slice) and includes, in addition to all the methods of slice, the additional:\n\n- `Sum` calculates the sum\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Sum([]float64{1, 3.14})  // 4.14\n  ```\n\n  \u003c/details\u003e\n\n- `Min` calculates the minimum value\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Min([]int{0, 1, -3})  // -3\n  ```\n\n  \u003c/details\u003e\n\n- `Max` calculates the maximum value\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Max([]int{0, 1, -3})  // 1\n  ```\n\n  \u003c/details\u003e\n\n- `Sort` sorts the numbers in the collection in ascending order\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Sort([]float64{1, -4, 0, -4.3})  // []float64{-4.3, -4, 0, 1}\n  ```\n\n  \u003c/details\u003e\n\n- `SortDesc` sorts the numbers in the collection in descending order\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.SortDesc([]float64{1, -4, 0, -4.3})  // []float64{1, 0, -4, -4.3}\n  ```\n\n  \u003c/details\u003e\n\n- `Avg` calculates the average\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Avg([]int{1, 2, 3, 4})  // 2.5\n  ```\n\n  \u003c/details\u003e\n\n- `Median` calculates the median\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Median([]int{1, 2, 3, 4})  // 2.5\n  ```\n\n  \u003c/details\u003e\n\n### Standalone functions\n\nDue to Golang's support for generics, it is [not possible to define generic types in methods](https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md#no-parameterized-methods), so only their function implementations (which do not support chain calls) are listed below:\n\n- `AnyGet` gets value of arbitrary types (slices, maps, arrays, structures, and pointers to these) in a non-strict form\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  m := map[string]int{\"a\": 1, \"b\": 2}\n  collect.AnyGet[int](m, \"b\")  // 2\n\n  u := \u0026User{\"Email\": \"user@example.com\"}\n  collect.AnyGet[string](u, \"Email\")  // user@example.com\n\n  s := [][]int{{1, 2}, {3, 4}}\n  collect.AnyGet[[]int](s, 1)  // []{3, 4}\n  ```\n\n  \u003c/details\u003e\n\n- `Pluck` retrieves all values for a given key. supports all values supported by `AnyGet`\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []User{{ID: 33, Name: \"Lucy\"}, {ID: 193, Name: \"Peter\"}}\n  collect.Pluck[int](d, \"ID\")  // int[]{33, 193}\n  ```\n\n  \u003c/details\u003e\n\n- `MapPluck` retrieves all values of a given key, only maps are supported\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []map[string]int{{\"ID\": 33, \"Score\": 10}, {\"ID\": 193, \"Score\": 6}}\n  collect.MapPluck(d, \"ID\")  // int[]{33, 193}\n  ```\n\n  \u003c/details\u003e\n\n- `KeyBy` retrieves a collection with the value of the given key as the identifier (if there are duplicate keys, only the last one will be kept). Supports all values supported by `AnyGet`\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []User{{ID: 33, Name: \"Lucy\"}, {ID: 193, Name: \"Peter\"}, {ID: 194, Name: \"Peter\"}}\n  collect.KeyBy[string](d, \"Name\")  // map[Lucy:{33 Lucy} Peter:{194 Peter}]\n  ```\n\n  \u003c/details\u003e\n\n- `MapKeyBy` retrieves the collection with the value of the given key as the identifier (if there are duplicate keys, only the last one will be kept), only maps are supported\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []map[string]int{{\"ID\": 33, \"Score\": 6}, {\"ID\": 193, \"Score\": 10}, {\"ID\": 194, \"Score\": 10}}\n  collect.MapKeyBy(d, \"Score\")  // map[6:map[ID:33 Score:6] 10:map[ID:194 Score:10]]\n  ```\n\n  \u003c/details\u003e\n\n- `GroupBy` groups the items in a collection using the value of the given key as the identifier. Supports all values supported by `AnyGet`\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []User{{ID: 33, Name: \"Lucy\"}, {ID: 193, Name: \"Peter\"}, {ID: 194, Name: \"Peter\"}}\n  collect.GroupBy[string](d, \"Name\")  // map[Lucy:[{33 Lucy}] Peter:[{193 Peter} {194 Peter}]]\n  ```\n\n  \u003c/details\u003e\n\n- `MapGroupBy` groups items in a collection using the value of the given key as the identifier, only maps are supported\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []map[string]int{{\"ID\": 33, \"Score\": 6}, {\"ID\": 193, \"Score\": 10}, {\"ID\": 194, \"Score\": 10}}\n  collect.MapGroupBy(d, \"Score\")  // map[6:[map[ID:33 Score:6]] 10:[map[ID:193 Score:10] map[ID:194 Score:10]]]\n  ```\n\n  \u003c/details\u003e\n\n- `Count` counts the number of occurrences of each element in the slice\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  d := []bool{true, true, false}\n  collect.Count(d)  // map[bool]int{true: 2, false: 1}\n  ```\n\n  \u003c/details\u003e\n\n- `Times` creates a new collection of slice by calling the callback with specified number of times\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.Times(3, func(number int) float64 {\n  \treturn float64(number) * 3.14\n  })  // *SliceCollection{[]float64{3.14, 6.28, 9.42}}\n  ```\n\n  \u003c/details\u003e\n\n- `SortBy` calls a callback for each element and performs an ascending sort by the return value of the callback\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.SortBy([]int{2, 1, 3}, func(item, index int) string {\n  \treturn strconv.Itoa(item)\n  })  // *SliceCollection{[]int{1, 2, 3}}\n  ```\n\n  \u003c/details\u003e\n\n- `SortByDesc` calls a callback for each element and performs a descending sort by the return value of the callback\n\n  \u003cdetails\u003e\n  \u003csummary\u003eExamples\u003c/summary\u003e\n\n  ```go\n  collect.SortByDesc([]int{2, 1, 3}, func(item, index int) string {\n  \treturn strconv.Itoa(item)\n  })  // *SliceCollection{[]int{3, 2, 1}}\n  ```\n\n  \u003c/details\u003e\n\n## License\n\ngo-collection is [MIT licensed](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsxyazi%2Fgo-collection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsxyazi%2Fgo-collection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsxyazi%2Fgo-collection/lists"}