Created
November 15, 2025 08:58
-
-
Save aliakakis/cca056db609bc46e6426bec84f065037 to your computer and use it in GitHub Desktop.
Go dynamic queue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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