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
19 Nov 2026
Golang Kafka Integration: Outbox Worker, Producer, Consumer Group E-Commerce
22 mnt
Read
Go
18 Nov 2026
Golang PostgreSQL Migration Strategy: Indexing, pgvector, Production-Ready
25 mnt
Read
Go
17 Nov 2026
Golang Notification Service: Kafka Consumer, Email Template, Event-Driven
25 mnt
Read
Go
16 Nov 2026
Golang Payment Service: HMAC Webhook, Idempotency, Outbox Pattern
21 mnt
Read