Skip to content

Instantly share code, notes, and snippets.

@trajber
Created September 14, 2013 20:11
Show Gist options
  • Save trajber/6565230 to your computer and use it in GitHub Desktop.
Save trajber/6565230 to your computer and use it in GitHub Desktop.
A simple implementation of a queue in Golang.
package main
import (
"fmt"
)
type Queue struct {
elements []interface{}
}
func NewQueue() *Queue {
q := new(Queue)
q.elements = make([]interface{}, 0)
return q
}
func (q *Queue) Enqueue(e interface{}) *Queue {
q.elements = append(q.elements, e)
return q
}
func (q *Queue) Dequeue() (e interface{}) {
e, q.elements = q.elements[0], q.elements[1:len(q.elements)]
return
}
func (q *Queue) Size() int {
return len(q.elements)
}
func main() {
q := NewQueue()
q.Enqueue(1).Enqueue(2).Enqueue(3).Enqueue("Hello")
for q.Size() > 0 {
fmt.Println(q.Dequeue())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment