⚡ TL;DR
Print the spiral maze path pattern in Go. Complete working code, expected output, algorithm explanation, and practice variations for beginners learning loops.
The Spiral Maze Path pattern is a classic loop exercise. Here’s the complete implementation in Go with working code and output.
Pattern to Print
##########
# #
# ###### #
# # # #
# # ## # #
# # ## # #
# # # #
# ###### #
# #
##########Implementation
package main
import "fmt"
func main() {
n := 10
maze := make([][]rune, n)
for i := range maze {
maze[i] = make([]rune, n)
for j := range maze[i] { maze[i][j] = ' ' }
}
top, bottom, left, right := 0, n-1, 0, n-1
for top <= bottom && left <= right {
for i := left; i <= right; i++ { maze[top][i] = '#' }
top += 2
for i := top; i <= bottom; i++ { maze[i][right] = '#' }
right -= 2
if top <= bottom {
for i := right; i >= left; i-- { maze[bottom][i] = '#' }
bottom -= 2
}
if left <= right {
for i := bottom; i >= top; i-- { maze[i][left] = '#' }
left += 2
}
}
for _, row := range maze { fmt.Println(string(row)) }
}Output
##########
# #
# ###### #
# # # #
# # ## # #
# # ## # #
# # # #
# ###### #
# #
##########How It Works
A spiral path drawn with walls: each pass adds a rectangular ring, then jumps 2 cells inward to start the next ring. Produces a clean maze-like spiral.
Practice Variations
- Try printing the pattern with a different character (e.g.,
#or+) - Modify the code to accept the pattern size as user input
- Combine this pattern with its inverse to create a more complex shape
Complexity
- Time: O(n²) — the grid or line count grows quadratically
- Space: O(1) — only loop counters are needed
Related Patterns
- Spiral Maze Path in Python
- Spiral Maze Path in Dart
- Spiral Maze Path in Swift
- Snake Matrix in Go
- Checkerboard in Go
- Hollow Cross Plus in Go
FAQ
How do you print the Spiral Maze Path pattern in Go?
Use nested loops: the outer loop walks through the rows while the inner loop prints the characters or values for each row. The complete Go implementation with expected output is shown in the sections above.
What is the time complexity of the Spiral Maze Path pattern in Go?
The time complexity is O(n²) and the space complexity is O(1), since the pattern is built with a fixed number of loop counters and printed row by row.
Happy coding!
