{"id":42395598,"url":"https://github.com/czasg/go-queue","last_synced_at":"2026-01-28T00:58:12.039Z","repository":{"id":39711692,"uuid":"424871716","full_name":"czasg/go-queue","owner":"czasg","description":"A thread-safe collections for memory/disk queues (FIFO), stacks (LIFO) and priority by Go.","archived":false,"fork":false,"pushed_at":"2022-05-28T03:47:17.000Z","size":42,"stargazers_count":9,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-06-21T05:01:45.166Z","etag":null,"topics":["disk-queue","fifo","go","golang","lifo","priority","queue"],"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/czasg.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":"2021-11-05T08:08:47.000Z","updated_at":"2024-06-21T05:01:45.166Z","dependencies_parsed_at":"2022-09-20T09:54:24.619Z","dependency_job_id":null,"html_url":"https://github.com/czasg/go-queue","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/czasg/go-queue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czasg%2Fgo-queue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czasg%2Fgo-queue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czasg%2Fgo-queue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czasg%2Fgo-queue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czasg","download_url":"https://codeload.github.com/czasg/go-queue/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czasg%2Fgo-queue/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28831004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T23:29:49.665Z","status":"ssl_error","status_checked_at":"2026-01-27T23:25:58.379Z","response_time":168,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: 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":["disk-queue","fifo","go","golang","lifo","priority","queue"],"created_at":"2026-01-28T00:58:11.336Z","updated_at":"2026-01-28T00:58:12.033Z","avatar_url":"https://github.com/czasg.png","language":"Go","readme":"# go-queue\n[![LICENSE](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square\u0026label=License)](https://github.com/czasg/go-queue/blob/master/LICENSE)\n[![codecov](https://codecov.io/gh/czasg/go-queue/branch/main/graph/badge.svg?token=GMXXOKC4P8)](https://codecov.io/gh/czasg/go-queue)\n[![GitHub Stars](https://img.shields.io/github/stars/czasg/go-queue.svg?style=flat-square\u0026label=Stars\u0026logo=github)](https://github.com/czasg/go-queue/stargazers)\n[![GitHub Forks](https://img.shields.io/github/forks/czasg/go-queue.svg?style=flat-square\u0026label=Forks\u0026logo=github)](https://github.com/czasg/go-queue/network/members)\n[![GitHub Issue](https://img.shields.io/github/issues/czasg/go-queue.svg?style=flat-square\u0026label=Issues\u0026logo=github)](https://github.com/czasg/go-queue/issues)\n\n## 1.背景\n在 go 中，存在 `chan` 这种天然的 FIFO 队列。  \n但类似 **LIFO、持久化** 等特殊结构/能力，并没有通用的标准库，更多的是借用三方软件来实现。 \n \ngo-queue 定义了简单的队列标准，提供了 **FIFO、LIFO、持久化** 等能力。\n\n## 2.目标\n1、内存队列、磁盘队列  \n- [x] FIFO Memory Queue - 内存队列\n- [x] LIFO Memory Queue - 内存队列\n- [x] FIFO Disk Queue - 磁盘队列\n- [x] LIFO Disk Queue - 磁盘队列\n\n2、Get/Put 支持阻塞，磁盘队列可不支持\n- [x] FIFO Block Memory Queue - 内存队列支持阻塞\n- [x] LIFO Block Memory Queue - 内存队列支持阻塞\n\n## 3.使用\n1、初始化队列\n```go\n// 依赖\nimport \"github.com/czasg/go-queue\"\n\n// 初始化内存队列\n_ = queue.NewFifoMemoryQueue() \n_ = queue.NewLifoMemoryQueue(2048) \n\n// 初始化磁盘队列，需要指定目标文件\nvar fifofilename, lifofilename string\n_, _ = queue.NewFifoDiskQueue(fifofilename)\n_, _ = queue.NewLifoDiskQueue(lifofilename)\n```\n\n2、推送数据\n```go\nq := queue.NewFifoMemoryQueue() \n// 非阻塞\n_ = q.Put(nil, []byte(\"data\"))\n// 阻塞\n_ = q.Put(context.Background(), []byte(\"data\"))\n```\n\n3、获取数据\n```go\nq := queue.NewFifoMemoryQueue() \n// 非阻塞\n_, _ = q.Get(nil)\n// 阻塞\n_, _ = q.Get(context.Background())\n```\n\n4、关闭队列\n```go\nq := queue.NewFifoMemoryQueue() \nq.Close()\n```\n特别是磁盘队列，使用完后务必确保关闭。否则会出现**文件损坏**问题。\n\n## 4.队列接口\n```\ntype Queue interface {\n    Get(ctx context.Context) ([]byte, error)\n    Put(ctx context.Context, data []byte) error\n    Len() int\n    Close() error\n}\n```\n其中上下文`context.Context`用于决定此次`Get / Put`是否阻塞。\n\n## 5.Demo\n### FIFO Memory Queue\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"github.com/czasg/go-queue\"\n    \"time\"\n)\n\nfunc main() {\n    q := queue.NewFifoMemoryQueue()\n    _ = q.Put(nil, []byte(\"go-queue\"))\n    data, _ := q.Get(nil)\n    fmt.Println(\"非阻塞获取数据\", string(data))\n\n    go func() {\n        data, _ := q.Get(context.Background())\n        fmt.Println(\"阻塞获取数据\", string(data))\n    }()\n    time.Sleep(time.Second * 2)\n    _ = q.Put(nil, []byte(\"go-queue\"))\n    time.Sleep(time.Millisecond)\n    q.Close()\n}\n```\n\n### LIFO Memory Queue\n```go\npackage main\n\nimport (\n    \"context\"\n    \"fmt\"\n    \"github.com/czasg/go-queue\"\n    \"time\"\n)\n\nfunc main() {\n    q := queue.NewLifoMemoryQueue()\n    _ = q.Put(nil, []byte(\"go-queue\"))\n    data, _ := q.Get(nil)\n    fmt.Println(\"非阻塞获取数据\", string(data))\n\n    go func() {\n        data, _ := q.Get(context.Background())\n        fmt.Println(\"阻塞获取数据\", string(data))\n    }()\n    time.Sleep(time.Second * 2)\n    _ = q.Put(nil, []byte(\"go-queue\"))\n    time.Sleep(time.Millisecond)\n    q.Close()\n}\n```\n\n### FIFO Disk Queue\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/czasg/go-queue\"\n    \"io/ioutil\"\n    \"os\"\n)\n\nfunc main() {\n    file, err := ioutil.TempFile(\"\", \"\")\n    if err != nil {\n        panic(err)\n    }\n    defer os.RemoveAll(file.Name())\n    file.Close()\n\n    q, _ := queue.NewFifoDiskQueue(file.Name())\n    _ = q.Put(nil, []byte(\"go-queue\"))\n    q.Close()\n\n    q, _ = queue.NewFifoDiskQueue(file.Name())\n    data, _ := q.Get(nil)\n    fmt.Println(\"获取数据\", string(data))\n    q.Close()\n}\n```\n\n### LIFO Disk Queue\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"github.com/czasg/go-queue\"\n    \"io/ioutil\"\n    \"os\"\n)\n\nfunc main() {\n    file, err := ioutil.TempFile(\"\", \"\")\n    if err != nil {\n        panic(err)\n    }\n    defer os.RemoveAll(file.Name())\n    file.Close()\n\n    q, _ := queue.NewLifoDiskQueue(file.Name())\n    _ = q.Put(nil, []byte(\"go-queue\"))\n    q.Close()\n\n    q, _ = queue.NewLifoDiskQueue(file.Name())\n    data, _ := q.Get(nil)\n    fmt.Println(\"获取数据\", string(data))\n    q.Close()\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczasg%2Fgo-queue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczasg%2Fgo-queue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczasg%2Fgo-queue/lists"}