# Golang program to print a matrix in Spiral Format

Draw 2D matrix, print all elements of the given matrix in Spiral format. Given a number n, print a n x n spiral matrix (of numbers from 1 to n x n) in clockwise direction using O(1) space.

### Example

```
``````
package main
import (
"fmt"
)
func spiral(n int) []int {
left, top, right, bottom := 0, 0, n-1, n-1
sz := n * n
s := make([]int, sz)
i := 0
for left < right {
// work right, along top
for c := left; c <= right; c++ {
s[top*n+c] = i
i++
}
top++
// work down right side
for r := top; r <= bottom; r++ {
s[r*n+right] = i
i++
}
right--
if top == bottom {
break
}
// work left, along bottom
for c := right; c >= left; c-- {
s[bottom*n+c] = i
i++
}
bottom--
// work up left side
for r := bottom; r >= top; r-- {
s[r*n+left] = i
i++
}
left++
}
// center (last) element
s[top*n+left] = i
return s
}
func main() {
num := 5
len := 2
for i, draw := range spiral(num) {
fmt.Printf("%*d ", len, draw)
if i%num == num-1 {
fmt.Println("")
}
}
}
```

##### Most Helpful This Week

Different ways for Integer to String Conversions
Concurrency in Golang
Web Application to generate QR code in Golang
Example: How to use ReadFull from IO Package in Golang?
What is the default HTTP server in Go?
Golang program for implementation of Median of Medians
Illustration of Producer Consumer Problem in Golang
How to create an empty Slice in Golang?
Golang program for implementation of Interpolation Search
What is Slice Data Type in Go?