15 Apr 2021
·
2 min read
·Article 11 / 119
GoHow to implement queue (queue) in Go Language
IH
Ihsan Arif
Writer at Santekno · Backend Engineer
Basic Definition
Queue or often we know is a queue data structure where the data we enter will be delivered, in other words the first data will come out first. Usually we often know that Istra FIFO (First in First Out).
According to information that Santekno can, Queue or queue is a collection of data whose additional elements can only be done at one end (called on the back side or rear), and delete or take elements done through another end (called the front or front side).
Queue Implementation
1package main
2
3type Queue struct {
4 items []int
5}
6
7func (q *Queue) Enqueue(i int) {
8 q.items = append(q.items, i)
9}
10
11func (q *Queue) Dequeue() int {
12 if len(q.items) == 0 {
13 return -1
14 }
15 item, items := q.items[0], q.items[1:]
16 q.items = items
17 return item
18}
19
20func main() {
21 q := Queue{}
22 q.Enqueue(1)
23 q.Enqueue(2)
24 q.Enqueue(3)
25
26 println(q.Dequeue())
27 println(q.Dequeue())
28 println(q.Dequeue())
29}Queue Implementation with Channel
1package main
2
3type Queue struct {
4 items chan int
5}
6
7func (q *Queue) Enqueue(i int) {
8 q.items <- i
9}
10
11func (q *Queue) Dequeue() int {
12 return <-q.items
13}
14
15func main() {
16 q := Queue{
17 items: make(chan int, 16),
18 }
19 q.Enqueue(1)
20 q.Enqueue(2)
21 q.Enqueue(3)
22
23 println(q.Dequeue())
24 println(q.Dequeue())
25 println(q.Dequeue())
26}Explanation
If you have seen how to implement the queue, the core of the data structure there are 2 enqueue operations, namely entering data into the rear elements (rear) and dequeue which is to retrieve data in the front side elements (front).
Related Articles
Go
14 Aug 2026
The .specify Folder Structure: Anatomy of the Generated Output
13 mnt
Read
Go
13 Aug 2026
Installing the specify CLI: Persistent vs One-time Setup
10 mnt
Read
Go
12 Aug 2026
What Is GitHub Spec Kit and Where It Fits in the SDD Workflow
13 mnt
Read
Go
11 Aug 2026
After SDD Golang: Deeper Tools and the Future of AI-Driven Development
12 mnt
Read