{"id":13563075,"url":"https://github.com/drgrib/maps","last_synced_at":"2025-10-15T13:02:05.866Z","repository":{"id":57480932,"uuid":"166703112","full_name":"drgrib/maps","owner":"drgrib","description":"Deprecated with note on how to implement this package's functionality using Go 1.18. Previously: Type-safe implementations of common map functions and tools to generate those functions.","archived":false,"fork":false,"pushed_at":"2022-03-18T16:21:03.000Z","size":26,"stargazers_count":14,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-23T18:54:22.045Z","etag":null,"topics":["generate","go","golang","map","maps"],"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/drgrib.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":"2019-01-20T19:58:08.000Z","updated_at":"2022-03-18T16:26:18.000Z","dependencies_parsed_at":"2022-09-26T22:11:36.801Z","dependency_job_id":null,"html_url":"https://github.com/drgrib/maps","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/drgrib%2Fmaps","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drgrib%2Fmaps/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drgrib%2Fmaps/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/drgrib%2Fmaps/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/drgrib","download_url":"https://codeload.github.com/drgrib/maps/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248073068,"owners_count":21043359,"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":["generate","go","golang","map","maps"],"created_at":"2024-08-01T13:01:14.906Z","updated_at":"2025-10-15T13:02:00.817Z","avatar_url":"https://github.com/drgrib.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.com/drgrib/maps.svg?branch=master)](https://travis-ci.com/drgrib/maps)\n\n# Deprecated\nWith the release of [Go 1.18](https://go.dev/blog/go1.18), this entire package can thankfully be replaced with the functions demonstrated below:\n\n``` go\npackage main\n\nimport (\n    \"fmt\"\n\n    \"golang.org/x/exp/maps\"\n)\n\nfunc ContainsKey[K, V comparable](m map[K]V, target V) bool {\n    for _, v := range m {\n        if v == target {\n            return true\n        }\n    }\n    return false\n}\n\nfunc main() {\n    m := map[string]int{\n        \"a\": 1,\n        \"b\": 2,\n        \"c\": 3,\n    }\n    fmt.Println(maps.Keys(m))\n    fmt.Println(maps.Values(m))\n\n    m2 := map[string]int{}\n    maps.Copy(m2, m)\n    fmt.Println(maps.Keys(m2))\n    fmt.Println(maps.Values(m2))\n\n    fmt.Println(ContainsKey(m2, 3))\n    fmt.Println(ContainsKey(m2, 4))\n}\n```\n\n# Installation\n\n``` bash\ngo get https://github.com/drgrib/maps\n```\n\n# Purpose\nThis package provides type-safe implementations of \"missing\" `map` functions:\n\n- `ContainsKey` - check if map contains key\n- `ContainsValue` - check if map contains value\n- `GetKeys` - get keys of a map\n- `GetValues` - get values of a map\n- `Copy` - deep copy of a map\n\nimplemented with these patterns\n\n- `ContainsKeyKV(map[ktype]vtype, k ktype) bool`\n- `ContainsValueKV(map[ktype]vtype, v vtype) bool`\n- `GetKeysKV(map[ktype]vtype) []ktype`\n- `GetValuesKV(map[ktype]vtype) []vtype`\n- `CopyKV(map[ktype]vtype) map[ktype]vtype`\n\nwhere `K` and `V` are the key and value for maps combining Go basic types, including but not limited to:\n\n- `StringString`\n- `StringInt`\n- `IntString`\n- `StringFloat32`\n- `StringFloat64`\n- `Float32Bool`\n- `Float64Bool`\n- `IntBool`\n- `StringBool`\n\n# Generation\nThe package extends beyond implementing these functions only for these basic type pairings. You can generate them for any `map` type, including maps with keys and values that are pointers, custom types, or `interface{}`.\n\nThere are two ways to do this. One is by installing and calling the `mapper` tool, which can be considered a sort of sibling to the [`stringer` tool](https://godoc.org/golang.org/x/tools/cmd/stringer). The other is programmatically by using the `maptemplate` library called within the [source code of `mapper`](https://github.com/drgrib/maps/blob/master/cmd/mapper/mapper.go) and [the code used to generate](https://github.com/drgrib/maps/blob/master/generate/main.go) the basic type `map` functions in this package.\n\n## `mapper`\n\n`mapper` can be installed using\n\n``` bash\ngo get -u github.com/drgrib/maps/cmd/mapper\n```\n\nThen used with the `go generate` like this:\n\n``` go\n//go:generate mapper -types string:CustomType\n```\n\nOr directly on the commandline with the same command:\n\n``` bash\nmapper -types string:CustomType\n```\n\nWhich will generate the file `map_string_customtype.go` that infers its `package` from surrounding `.go` files or the current folder name if no other files are found:\n\n``` go\npackage maps\n\nfunc ContainsKeyStringCustomType(m map[string]CustomType, k string) bool {\n\t_, ok := m[k]\n\treturn ok\n}\n\nfunc ContainsValueStringCustomType(m map[string]CustomType, v CustomType) bool {\n\tfor _, mValue := range m {\n\t\tif mValue == v {\n\t\t\treturn true\n\t\t}\n\t}\n\n\treturn false\n}\n\nfunc GetKeysStringCustomType(m map[string]CustomType) []string {\n\tkeys := []string{}\n\n\tfor k, _ := range m {\n\t\tkeys = append(keys, k)\n\t}\n\n\treturn keys\n}\n\nfunc GetValuesStringCustomType(m map[string]CustomType) []CustomType {\n\tvalues := []CustomType{}\n\n\tfor _, v := range m {\n\t\tvalues = append(values, v)\n\t}\n\n\treturn values\n}\n\nfunc CopyStringCustomType(m map[string]CustomType) map[string]CustomType {\n\tcopyMap := map[string]CustomType{}\n\n\tfor k, v := range m {\n\t\tcopyMap[k] = v\n\t}\n\n\treturn copyMap\n}\n```\n\n# About Generics\nIf generics are implemented for Go 2.0, these functions can be covered by a single file, without the need for the `mapper` tool and its underlying generation packages. Until then, there is this package.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrgrib%2Fmaps","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdrgrib%2Fmaps","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdrgrib%2Fmaps/lists"}