Skip to content

Instantly share code, notes, and snippets.

@aliakakis
Created November 15, 2025 08:58
Show Gist options
  • Select an option

  • Save aliakakis/cca056db609bc46e6426bec84f065037 to your computer and use it in GitHub Desktop.

Select an option

Save aliakakis/cca056db609bc46e6426bec84f065037 to your computer and use it in GitHub Desktop.
Go dynamic queue
/*
Usage
q := NewQueue()
q.EnQueue(func() {
time.Sleep(2 * time.Second)
fmt.Println("Hello")
})
q.EnQueue(func() {
fmt.Println("World")
})
q.run()
*/
package main
import (
"fmt"
"slices"
"time"
)
type Queue struct {
tasks []func()
}
func NewQueue() *Queue {
q := Queue{
tasks: make([]func(), 0),
}
return &q
}
func (q *Queue) EnQueue(fn func()) {
q.tasks = append(q.tasks, fn)
}
func (q *Queue) run() {
for len(q.tasks) > 0 {
fn := q.tasks[0]
q.tasks = slices.Delete(q.tasks, 0, 1)
fn()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment