{"id":29151248,"url":"https://github.com/karpeleslab/weak","last_synced_at":"2025-07-01T00:08:59.006Z","repository":{"id":57695397,"uuid":"489594766","full_name":"KarpelesLab/weak","owner":"KarpelesLab","description":"Weak reference map for Go 1.18+","archived":false,"fork":false,"pushed_at":"2022-05-13T16:19:08.000Z","size":9,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-01T00:08:56.265Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/KarpelesLab.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-05-07T06:52:19.000Z","updated_at":"2025-03-28T16:39:31.000Z","dependencies_parsed_at":"2022-09-02T10:22:20.225Z","dependency_job_id":null,"html_url":"https://github.com/KarpelesLab/weak","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/KarpelesLab/weak","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fweak","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fweak/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fweak/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fweak/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KarpelesLab","download_url":"https://codeload.github.com/KarpelesLab/weak/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KarpelesLab%2Fweak/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262870877,"owners_count":23377314,"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":"2025-07-01T00:08:55.538Z","updated_at":"2025-07-01T00:08:58.970Z","avatar_url":"https://github.com/KarpelesLab.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![GoDoc](https://godoc.org/github.com/KarpelesLab/weak?status.svg)](https://godoc.org/github.com/KarpelesLab/weak)\n\n# weakref map in go 1.18\n\nThis is a weakref map for Go 1.18, with some inspiration from [xeus2001's weakref implementation](https://github.com/xeus2001/go-weak).\n\nThis provides both a `weak.Ref` object to store weak references, and a `weak.Map` object for maps.\n\n## Usage\n\n```go\nimport \"github.com/KarpelesLab/weak\"\n\nvar m = weak.NewMap[uint64, Object]()\n\n// instanciate/get an object\nfunc Get(id uint64) (*Object, error) {\n\t// try to get from cache\n\tvar obj *Object\n\tif obj = m.Get(id); obj == nil {\n\t\t// create new\n\t\tobj = m.Set(id, \u0026Object{id: id}) // this will return an existing object if already existing\n\t}\n\n\tobj.initOnce.Do(obj.init) // use sync.Once to ensure init happens only once\n\treturn obj, obj.err\n}\n\nfunc main() {\n\tobj, err := Get(1234)\n\t// ...\n}\n```\n\nAs to the `Object` implementation, it could look like:\n\n```go\ntype Object struct {\n\tid       uint64\n\tinitOnce sync.Once\n\tf        *os.File\n\terr      error\n}\n\nfunc (o *Object) init() {\n\to.f, o.err = os.Open(fmt.Sprintf(\"/tmp/file%d.bin\", o.id))\n}\n\nfunc (o *Object) Destroy() {\n\tif o.f != nil {\n\t\to.f.Close()\n\t}\n}\n```\n\n### File example\n\nSimple example that allows opening multiple files without having to care about closing these and reading random data using `ReadAt`.\n\n```go\nimport {\n\t\"github.com/KarpelesLab/weak\"\n\t\"os\"\n\t\"sync\"\n}\n\ntype File struct {\n\t*os.File\n\terr  error\n\topen sync.Once\n}\n\nvar fileCache = weak.NewMap[string, File]()\n\nfunc Open(filepath string) (io.ReaderAt, error) (\n\tvar f *File\n\tif f = fileCache.Get(filepath); f == nil {\n\t\tf = fileCache.Set(filepath, \u0026File{})\n\t}\n\tf.open.Do(func() {\n\t\tf.File, f.err = os.Open(filepath)\n\t})\n\treturn f, f.err\n}\n\nfunc (f *File) Destroy() {\n\tif f.File != nil {\n\t\tf.File.Close()\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarpeleslab%2Fweak","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarpeleslab%2Fweak","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarpeleslab%2Fweak/lists"}