Ad space (banner)
๐ŸนGo Lessons
Lesson 30 / 68

Stacks and Queues (Basic Data Structures)

In this lesson you will learn how to build stacks and queues in Go, and understand which way of adding and removing data suits which job. This is for anyone searching for "Go stack queue how to use".

A stack is "last in, first out" and a queue is "first in, first out". The difference between a pile of books and a queue of people is a helpful picture.

The sample code pushes values with stack = append(stack, "A"), reads the last one with stack[len(stack)-1], and shrinks the slice with stack[:len(stack)-1]. The queue instead takes the front value queue[0] and keeps the rest with queue[1:].

A common stumbling block for beginners is that Go has no dedicated types for these; you build them yourself from a slice with append and slice operations. Keep in mind that taking from the front or the back is exactly what separates a queue from a stack.

In real-world development the stack underpins the mechanism of function calls itself, making it an important structure that also builds your computer science foundations.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)