⚡ TL;DR
10 pattern programs in Go — spiral matrix, hollow diamond, letter triangle, Fibonacci triangle, wave, plus sign, and more. Full code and output.
A follow-up to our Go pattern collection, this post covers 10 more patterns commonly asked in Go interviews. Each includes working code and output.
1. Spiral Matrix
Fill an n x n grid with numbers in spiral order.
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7package main
import "fmt"
func spiralMatrix(n int) {
m := make([][]int, n)
for i := range m {
m[i] = make([]int, n)
}
num := 1
top, bottom, left, right := 0, n-1, 0, n-1
for top <= bottom && left <= right {
for i := left; i <= right; i++ {
m[top][i] = num
num++
}
top++
for i := top; i <= bottom; i++ {
m[i][right] = num
num++
}
right--
for i := right; i >= left; i-- {
m[bottom][i] = num
num++
}
bottom--
for i := bottom; i >= top; i-- {
m[i][left] = num
num++
}
left++
}
for _, row := range m {
for _, v := range row {
fmt.Printf("%2d ", v)
}
fmt.Println()
}
}
func main() {
spiralMatrix(4)
}Output
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 72. Hollow Diamond
Diamond outline with stars.
*
* *
* *
* *
*package main
import "fmt"
func hollowDiamond(n int) {
for i := 1; i <= n; i++ {
for j := 0; j < n-i; j++ {
fmt.Print(" ")
}
if i == 1 {
fmt.Print("*")
} else {
fmt.Print("*")
for j := 0; j < 2*i-3; j++ {
fmt.Print(" ")
}
fmt.Print("*")
}
fmt.Println()
}
for i := n - 1; i >= 1; i-- {
for j := 0; j < n-i; j++ {
fmt.Print(" ")
}
if i == 1 {
fmt.Print("*")
} else {
fmt.Print("*")
for j := 0; j < 2*i-3; j++ {
fmt.Print(" ")
}
fmt.Print("*")
}
fmt.Println()
}
}
func main() {
hollowDiamond(3)
}Output
*
* *
* *
* *
*3. Letter Triangle
Rows of consecutive letters.
A
A B
A B C
A B C D
A B C D Epackage main
import (
"fmt"
"strings"
)
func letterTriangle(n int) {
for i := 0; i < n; i++ {
var row []string
for j := 0; j <= i; j++ {
row = append(row, string(rune('A'+j)))
}
fmt.Println(strings.Join(row, " "))
}
}
func main() {
letterTriangle(5)
}Output
A
A B
A B C
A B C D
A B C D E4. Number Stair
Row i repeats the number i.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5package main
import (
"fmt"
"strconv"
"strings"
)
func numberStair(n int) {
for i := 1; i <= n; i++ {
var row []string
for j := 0; j < i; j++ {
row = append(row, strconv.Itoa(i))
}
fmt.Println(strings.Join(row, " "))
}
}
func main() {
numberStair(5)
}Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 55. Fibonacci Triangle
Fibonacci numbers continue across rows.
0
1 1
2 3 5
8 13 21 34
55 89 144 233 377package main
import (
"fmt"
"strconv"
"strings"
)
func fibonacciTriangle(n int) {
a, b := 0, 1
for i := 1; i <= n; i++ {
var row []string
for j := 0; j < i; j++ {
row = append(row, strconv.Itoa(a))
a, b = b, a+b
}
fmt.Println(strings.Join(row, " "))
}
}
func main() {
fibonacciTriangle(5)
}Output
0
1 1
2 3 5
8 13 21 34
55 89 144 233 3776. Binary Triangle
Alternating 1s and 0s.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1package main
import (
"fmt"
"strconv"
"strings"
)
func binaryTriangle(n int) {
for i := 1; i <= n; i++ {
var row []string
for j := 1; j <= i; j++ {
row = append(row, strconv.Itoa((1+i+j)%2))
}
fmt.Println(strings.Join(row, " "))
}
}
func main() {
binaryTriangle(5)
}Output
1
0 1
1 0 1
0 1 0 1
1 0 1 0 17. Plus Sign
A + made of stars.
*
*
*****
*
*package main
import "fmt"
func plusSign(n int) {
mid := n / 2
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i == mid || j == mid {
fmt.Print("*")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
func main() {
plusSign(5)
}Output
*
*
*****
*
*8. Hollow Pyramid
A pyramid with hollow inside.
*
* *
* *
* *
*********package main
import "fmt"
func hollowPyramid(n int) {
for i := 1; i <= n; i++ {
for j := 0; j < n-i; j++ {
fmt.Print(" ")
}
if i == n {
for j := 0; j < 2*n-1; j++ {
fmt.Print("*")
}
} else if i == 1 {
fmt.Print("*")
} else {
fmt.Print("*")
for j := 0; j < 2*i-3; j++ {
fmt.Print(" ")
}
fmt.Print("*")
}
fmt.Println()
}
}
func main() {
hollowPyramid(5)
}Output
*
* *
* *
* *
*********9. X-Shape
Diagonals of stars forming an “X”.
* *
* *
*
* *
* *package main
import "fmt"
func xShape(n int) {
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
if i == j || i+j == n-1 {
fmt.Print("*")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
func main() {
xShape(5)
}Output
* *
* *
*
* *
* *10. Sandglass
An inverted triangle on top of a triangle.
*****
***
*
***
*****package main
import "fmt"
func sandglass(n int) {
for i := n; i >= 1; i-- {
for j := 0; j < n-i; j++ {
fmt.Print(" ")
}
for k := 0; k < 2*i-1; k++ {
fmt.Print("*")
}
fmt.Println()
}
for i := 2; i <= n; i++ {
for j := 0; j < n-i; j++ {
fmt.Print(" ")
}
for k := 0; k < 2*i-1; k++ {
fmt.Print("*")
}
fmt.Println()
}
}
func main() {
sandglass(3)
}Output
*****
***
*
***
*****Conclusion
These Go patterns complete the trifecta of pattern posts on the site. Combine them with the earlier unique patterns in Go post for a complete beginner’s reference.
Happy coding!

Written by Mihir Pipermitwala
Senior Full Stack Consultant & Mobile Engineering Specialist building user-focused applications in healthcare with Flutter, Dart and React Native. I write about what I actually build and test — every tutorial here comes from real project work.
More about Mihir →