Go slice exercises
In Go, slices are three-word headers pointing to underlying arrays. Subtle bugs occur when append reallocates unexpectedly, copy fails because the destination length is zero, or sub-slicing shares mutated memory across functions.
Each exercise is a focused Go function with one real bug and automated tests to verify your fix.
The slices bugs you will meet
- copy(dst, src) copies only up to min(len(dst), len(src)); an unallocated dst copies zero elements.
- append() returns a new slice header that must be reassigned back to the variable.
- Sub-slices s[a:b] share the backing array until an append exceeds capacity.
- len(s) is the number of elements; the last item is at len(s) - 1.
- A nil slice has len 0 and cap 0; append works, but direct index assignment panics.
The exercises
Easy
Medium
- Unexpected behavior when appending to a Go sliceLogic
- Unexpected output in concurrent range loop processingAsync and race conditions
- Stalled Goroutine: Data Not Received as ExpectedAsync and race conditions
- Goroutines finish before expected despite using sync.WaitGroupAsync and race conditions
- Zero-length slice copy in data transferLogic
- JSON Unmarshal fails to populate struct fields silentlyType
- Worker function not exiting properly causing timeoutsAsync and race conditions
- Unexpected stale data appears in slice processingLogic
- Consistent Panic in Channel HandlingAsync and race conditions
- Panic during channel operation in concurrent Go programAsync and race conditions
- Unexpected zero value in concurrent Go programAsync and race conditions
- Unpredictable behavior in concurrent message handlingAsync and race conditions
- Random sequence repeats identically across multiple runsMutation and copying
- UTF-8 multi-byte rune indexing errorType
- Deadlock on unbuffered channel without receiverAsync and race conditions
- Deadlock in range loop over unclosed channelInfinite loops
- JSON Unmarshal silent failure with lowercase fieldsLogic
- Panic on closing already closed Go channelAsync and race conditions
- Negative modulo index out of range in hash bucketLogic
- Slice append mutation drops across function callLogic
- Loop variable captured by reference in goroutineAsync and race conditions
- Truncating multi-byte string by raw byte sliceLogic
- Sort Slice comparator violates strict weak orderingLogic
- Select default branch starves channel communicationAsync and race conditions
Errors you might be searching for
More JavaScript practice by topic
Questions
Do I need Go installed?
No. Test verification runs through BugHunt's instant execution runner.
What makes Go slices tricky?
A slice is a lightweight view over an array. Passing a slice to a function passes the header by value, but modifying existing indices mutates the shared underlying storage.