8000 GitHub - nickalie/go-queue: Multi backend queues for Golang · GitHub
[go: up one dir, main page]

Skip to content
nickaliePublic

About

Multi backend queues for Golang

Topics

Resources

Stars

19 stars

Watchers

2 watching

Forks

Latest commit

 

History

34 Commits

Folders and files

Repository files navigation

Go Queue

Multi backend queues for Golang

CI codecov Codacy BadgeGo Report Card

Install

go get github.com/nickalie/go-queue

Type-safe API

A generic Queue[T] wrapper provides compile-time type safety on top of any backend:

q := queue.New[string]("messages")

if err := q.Put("hello"); err != nil {
	log.Fatal(err)
}

msg, err := q.Get() // msg is a string, no pointers or type assertions
if err != nil {
	log.Fatal(err)
}

fmt.Println(msg)

Example of usage

package main

import (
	"fmt"
	"github.com/nickalie/go-queue"
	"time"
)

func main() {
	for i := 0; i < 5; i++ {
		go consumer(i + 1)
	}

	producer()
}

func producer() {
	i := 0
	for {
		i++
		queue.Put("messages", fmt.Sprintf("message %d", i))
		time.Sleep(time.Second)
	}
}

func consumer(index int) {
	for {
		var message string
		queue.Get("messages", &message)

		fmt.Printf("Consumer %d got a message: %s\n", index, message)
		time.Sleep(2 * time.Second)
	}
}

About

Multi backend queues for Golang

Topics

Resources

Stars

19 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages

0