{"id":782,"date":"2026-10-07T09:00:00","date_gmt":"2026-10-07T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/"},"modified":"2026-11-29T01:29:35","modified_gmt":"2026-11-28T19:59:35","slug":"go-matrix-programs","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/","title":{"rendered":"Go Matrix Programs: Addition, Multiplication, Transpose, Rotate"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go matrix programs work with rows and columns of numbers, like a spreadsheet, a game board or the pixels of an image. In this post, you will write fourteen matrix programs with their exact output, including addition, multiplication, transpose, rotation and spiral order. Then you will practice the matrix questions that come up in coding rounds, where index mistakes are very common.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This post closes Level 3 of our Go programs series. It uses the 2D slices from our <a href=\"https:\/\/smitabharat.com\/blog\/go-slice-programs\/\">Go slice programs<\/a> and the 2D arrays from our <a href=\"https:\/\/smitabharat.com\/blog\/go-array-programs\/\">Go array programs<\/a>. It also uses the nested loops from our <a href=\"https:\/\/smitabharat.com\/blog\/go-pattern-programs\/\">Go pattern programs<\/a>. All programs were tested on Go 1.27.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Go Matrix Programs?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go matrix programs store numbers in a grid with rows and columns. In Go, a matrix is usually a slice of slices, <code>[][]int<\/code>, or a fixed 2D array such as <code>[3][3]int<\/code>. You reach one cell with two indexes: <code>m[row][col]<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a cinema seating chart. Each row has a number, and each seat in that row has a number. To find your seat, you need both. A matrix works the same way, with the row first and the column second.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Matrix Programs Matter<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Grids appear in many real problems. For example, a chess or tic-tac-toe board is a grid, an image is a grid of pixels and a timetable is a grid of days and periods. Interviewers like Go matrix programs because they test careful loops and index maths.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Interviews:<\/strong> rotate a matrix, spiral order and search a 2D grid are classic questions.<\/li>\n\n\n<li><strong>Games and maps:<\/strong> boards, mazes and tile maps all use 2D grids.<\/li>\n\n\n<li><strong>Maths and data:<\/strong> tables, images and machine learning use matrix operations.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Matrix Programs Work<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most Go matrix programs use two nested loops. The outer loop walks the rows, and the inner loop walks the columns of the current row. You get the number of rows with <code>len(m)<\/code> and the number of columns with <code>len(m[0])<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-1-1024x640.png\" alt=\"Go matrix programs diagram showing rows, columns and the m row col index\" class=\"wp-image-757\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Each cell is found with m[row<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The table below shows the common ways to create a matrix in Go.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Way<\/th><th>Example<\/th><th>When to use<\/th><\/tr><\/thead><tbody><tr><td>2D array<\/td><td>var board [3][3]int<\/td><td>Fixed size known at compile time<\/td><\/tr><tr><td>Slice literal<\/td><td>m := [][]int{{1, 2}, {3, 4}}<\/td><td>Small known data<\/td><\/tr><tr><td>make in a loop<\/td><td>m[i] = make([]int, cols)<\/td><td>Size known at run time<\/td><\/tr><tr><td>One flat slice<\/td><td>data[r*cols+c]<\/td><td>Big grids, best speed<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Go Matrix Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These first Go matrix programs create, print and add matrices. If you are new to grids, start with Program 1.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 1: Your First Matrix and m[row][col]<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This easy program reads one cell, then prints every cell with two loops, just like the diagram above.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ m is a matrix: a slice of rows, and each row is a slice of numbers.\n    m := [][]int{ \/\/ := creates the variable and sets its value\n        {1, 2, 3}, \/\/ row 0\n        {4, 5, 6}, \/\/ row 1\n    }\n\n    \/\/ Step 1: read one cell. The row comes first, then the column.\n    fmt.Println(\"Cell at row 1, col 2:\", m[1][2])\n\n    \/\/ Step 2: visit every cell with two loops.\n    for row := 0; row &lt; len(m); row++ { \/\/ len(m) is the number of rows\n        for col := 0; col &lt; len(m[row]); col++ { \/\/ len(m[row]) is the number of columns\n            fmt.Print(m[row][col], \" \")\n        }\n        fmt.Println() \/\/ start a new line after each row\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Cell at row 1, col 2: 6\n1 2 3\n4 5 6<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Indexes start at 0, so <code>m[1][2]<\/code> means the second row and the third column. That cell holds 6. The outer loop picks a row, and the inner loop prints each number in that row. After each row, <code>fmt.Println()<\/code> moves to the next line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 2: Create and Print a Matrix<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We write a helper that makes a matrix of any size, filled with zeros.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ newMatrix makes a grid with the given rows and columns, all set to 0.\nfunc newMatrix(rows, cols int) [][]int {\n    \/\/ Step 1: make the outer slice that will hold the rows.\n    matrix := make([][]int, rows) \/\/ make creates a slice of the given length\n    \/\/ Step 2: make each row separately.\n    for i := range matrix { \/\/ range gives each index 0, 1, 2 ...\n        matrix[i] = make([]int, cols)\n    }\n    return matrix\n}\n\nfunc main() {\n    matrix := newMatrix(2, 3)\n    matrix[0][1] = 5\n    matrix[1][2] = 9\n    for _, row := range matrix { \/\/ _ ignores the index, row is each row slice\n        fmt.Println(row)\n    }\n    fmt.Println(\"Rows:\", len(matrix), \"Cols:\", len(matrix[0]))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[0 5 0]\n[0 0 9]\nRows: 2 Cols: 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>make([][]int, rows)<\/code> creates the list of rows, but each row is still empty. Then the loop gives each row its own <code>make<\/code> call with room for the columns. If you skip that step, the rows stay nil and writing to them panics.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 3: Matrix Addition<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Two matrices can be added only if they have the same size. Each cell adds the matching cells.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    first := [][]int{{1, 2, 3}, {4, 5, 6}}\n    second := [][]int{{10, 20, 30}, {40, 50, 60}}\n\n    \/\/ Step 1: make the result with the same number of rows.\n    sum := make([][]int, len(first))\n    for i := range first {\n        \/\/ Step 2: make this row of the result.\n        sum[i] = make([]int, len(first[i]))\n        \/\/ Step 3: add the matching cells.\n        for j := range first[i] {\n            sum[i][j] = first[i][j] + second[i][j]\n        }\n    }\n    fmt.Println(sum)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[[11 22 33] [44 55 66]]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loop uses <code>i<\/code> for the row and <code>j<\/code> for the column. For each position, it adds the cell from <code>first<\/code> and the cell from <code>second<\/code>. Subtraction works exactly the same way with <code>-<\/code> instead of <code>+<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 4: Row Sums and Column Sums<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    \/\/ Each row is a shop, each column is a day.\n    sales := [][]int{\n        {120, 80, 95},\n        {60, 150, 70},\n        {90, 40, 110},\n    }\n\n    \/\/ One running total for each column.\n    columnTotals := make([]int, len(sales[0]))\n\n    for i, row := range sales {\n        rowTotal := 0 \/\/ starts again at 0 for every row\n        for j, value := range row {\n            rowTotal += value\n            columnTotals[j] += value \/\/ keeps adding across all rows\n        }\n        fmt.Println(\"Row\", i, \"total:\", rowTotal)\n    }\n    fmt.Println(\"Column totals:\", columnTotals)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Row 0 total: 295\nRow 1 total: 280\nRow 2 total: 240\nColumn totals: [270 270 275]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One pass fills both totals. The row total resets to 0 for each row, so it prints after the inner loop. The column totals keep adding across all rows, so we print them at the very end.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Classic Go Matrix Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go matrix programs cover the operations from school maths.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 5: Multiply Every Cell by a Number<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This is the easiest maths operation on a matrix. It is called scalar multiplication.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    prices := [][]int{{10, 20}, {30, 40}}\n\n    \/\/ Double every price, one cell at a time.\n    for i := range prices {\n        for j := range prices[i] {\n            prices[i][j] = prices[i][j] * 2\n        }\n    }\n    fmt.Println(prices)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[[20 40] [60 80]]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The two loops visit every cell once. Each cell is read, multiplied by 2 and written back in the same place. This warm-up uses the same loop shape as the bigger programs below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 6: Matrix Multiplication<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A 2&#215;3 matrix times a 3&#215;2 matrix gives a 2&#215;2 result. The columns of the first must equal the rows of the second.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"errors\"\n    \"fmt\"\n)\n\n\/\/ multiply returns first x second, or an error if the sizes do not fit.\nfunc multiply(first, second [][]int) ([][]int, error) {\n    \/\/ Step 1: check the sizes.\n    if len(first[0]) != len(second) {\n        return nil, errors.New(\"columns of first must equal rows of second\")\n    }\n    \/\/ Step 2: make the result, rows of first by columns of second.\n    result := make([][]int, len(first))\n    for i := range first {\n        result[i] = make([]int, len(second[0]))\n        \/\/ Step 3: each cell is a row of first times a column of second.\n        for j := range second[0] {\n            for k := range second {\n                result[i][j] += first[i][k] * second[k][j]\n            }\n        }\n    }\n    return result, nil \/\/ nil means \"no error\"\n}\n\nfunc main() {\n    first := [][]int{{1, 2, 3}, {4, 5, 6}}\n    second := [][]int{{7, 8}, {9, 10}, {11, 12}}\n    fmt.Println(multiply(first, second)) \/\/ prints both return values\n    fmt.Println(multiply(first, first))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[[58 64] [139 154]] &lt;nil&gt;\n[] columns of first must equal rows of second<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This function returns two things: the result and an error value. The first call works, so the error prints as <code>&lt;nil&gt;<\/code>. Each result cell is the sum of a row times a column, so 58 is 1&#215;7 + 2&#215;9 + 3&#215;11. The second call has the wrong sizes, so it returns the error message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 7: Transpose a Matrix<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The transpose turns rows into columns.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    matrix := [][]int{{1, 2, 3}, {4, 5, 6}}\n\n    \/\/ The result has as many rows as matrix has columns.\n    transposed := make([][]int, len(matrix[0]))\n    for j := range transposed {\n        transposed[j] = make([]int, len(matrix))\n        for i := range matrix {\n            transposed[j][i] = matrix[i][j] \/\/ row i, column j moves to row j, column i\n        }\n    }\n    fmt.Println(transposed)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[[1 4] [2 5] [3 6]]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A 2&#215;3 matrix becomes 3&#215;2. We build each new row by reading one column of the old matrix. The cell at row i, column j moves to row j, column i.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 8: Diagonal Sums<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    matrix := [3][3]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} \/\/ a fixed 3x3 array\n    size := len(matrix)\n    primary, secondary := 0, 0\n    for i := 0; i &lt; size; i++ {\n        primary += matrix[i][i]          \/\/ top left to bottom right\n        secondary += matrix[i][size-1-i] \/\/ top right to bottom left\n    }\n    fmt.Println(\"Primary:\", primary, \"Secondary:\", secondary)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Primary: 15 Secondary: 15<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On the main diagonal, the row and column numbers are equal, so we read <code>matrix[i][i]<\/code>. The other diagonal uses column <code>size-1-i<\/code>, which runs from right to left. One loop adds both totals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 9: Identity Matrix Check<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An identity matrix has 1 on the main diagonal and 0 everywhere else.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ isIdentity reports whether matrix has 1 on the diagonal and 0 elsewhere.\nfunc isIdentity(matrix [][]int) bool {\n    for i := range matrix {\n        for j := range matrix[i] {\n            \/\/ Step 1: work out what this cell should hold.\n            want := 0\n            if i == j {\n                want = 1\n            }\n            \/\/ Step 2: stop early if the cell is wrong.\n            if matrix[i][j] != want {\n                return false\n            }\n        }\n    }\n    return true\n}\n\nfunc main() {\n    fmt.Println(isIdentity([][]int{{1, 0}, {0, 1}}))\n    fmt.Println(isIdentity([][]int{{1, 2}, {0, 1}}))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true\nfalse<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For every cell, we decide the expected value first. A cell on the diagonal should be 1, and every other cell should be 0. The function returns false at the first wrong cell, and true only if all cells pass.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 10: Symmetric Matrix Check<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A matrix is symmetric when it equals its own transpose.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    matrix := [][]int{{2, 7, 3}, {7, 4, 5}, {3, 5, 6}}\n    symmetric := true\n    for i := range matrix {\n        \/\/ Start j at i + 1, so we only look above the diagonal.\n        for j := i + 1; j &lt; len(matrix); j++ {\n            if matrix[i][j] != matrix[j][i] {\n                symmetric = false\n            }\n        }\n    }\n    fmt.Println(\"Symmetric:\", symmetric)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Symmetric: true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Each cell above the diagonal must match its mirror cell below it. So we compare <code>matrix[i][j]<\/code> with <code>matrix[j][i]<\/code>. We only check cells above the diagonal, because each pair needs one comparison.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Interview Go Matrix Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These Go matrix programs show up often in coding rounds.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 11: Rotate a Matrix 90 Degrees Clockwise<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The trick is to transpose the matrix, then reverse each row.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n    \"fmt\"\n    \"slices\"\n)\n\n\/\/ rotate turns a square matrix 90 degrees clockwise, in place.\nfunc rotate(matrix [][]int) {\n    size := len(matrix)\n    \/\/ Step 1: transpose by swapping cells across the diagonal.\n    for i := 0; i &lt; size; i++ {\n        for j := i + 1; j &lt; size; j++ {\n            \/\/ Swap the two cells in one line: each side gets the other's old value.\n            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]\n        }\n    }\n    \/\/ Step 2: reverse each row.\n    for _, row := range matrix {\n        slices.Reverse(row)\n    }\n}\n\nfunc main() {\n    matrix := [][]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}\n    rotate(matrix)\n    for _, row := range matrix {\n        fmt.Println(row)\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[7 4 1]\n[8 5 2]\n[9 6 3]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After step 1, the first row is 1 4 7. Reversing it gives 7 4 1, which is the first row of the rotated matrix. This works in place, with no second matrix. For anticlockwise, reverse each row first and then transpose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 12: Spiral Order<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We walk the outer ring, then shrink the edges and repeat.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ spiral returns the cells of matrix in clockwise spiral order.\nfunc spiral(matrix [][]int) []int {\n    var result []int\n    \/\/ The four edges of the part we have not visited yet.\n    top, bottom := 0, len(matrix)-1\n    left, right := 0, len(matrix[0])-1\n    for top &lt;= bottom &amp;&amp; left &lt;= right {\n        \/\/ Step 1: go right along the top row.\n        for col := left; col &lt;= right; col++ {\n            result = append(result, matrix[top][col]) \/\/ append adds to the end\n        }\n        top++\n        \/\/ Step 2: go down the right column.\n        for row := top; row &lt;= bottom; row++ {\n            result = append(result, matrix[row][right])\n        }\n        right--\n        \/\/ Step 3: go left along the bottom row, if one is left.\n        if top &lt;= bottom {\n            for col := right; col &gt;= left; col-- {\n                result = append(result, matrix[bottom][col])\n            }\n            bottom--\n        }\n        \/\/ Step 4: go up the left column, if one is left.\n        if left &lt;= right {\n            for row := bottom; row &gt;= top; row-- {\n                result = append(result, matrix[row][left])\n            }\n            left++\n        }\n    }\n    return result\n}\n\nfunc main() {\n    matrix := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}\n    fmt.Println(spiral(matrix))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[1 2 3 4 8 12 11 10 9 5 6 7]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Four edge markers track the unvisited part of the grid. After walking each side, we move that edge one step inward. The two <code>if<\/code> checks stop us from reading a row or column twice when the matrix is not square.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 13: Largest Value and Its Position<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    scores := [][]int{{45, 78, 62}, {91, 55, 88}, {70, 99, 64}}\n\n    \/\/ Step 1: start by guessing the first cell is the biggest.\n    best := scores[0][0]\n    bestRow := 0\n    bestCol := 0\n\n    \/\/ Step 2: check every cell and remember a bigger one.\n    for row, cells := range scores {\n        for col, value := range cells {\n            if value &gt; best {\n                best = value\n                bestRow = row\n                bestCol = col\n            }\n        }\n    }\n    fmt.Printf(\"Max %d at row %d, column %d\\n\", best, bestRow, bestCol) \/\/ %d prints a whole number\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Max 99 at row 2, column 1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We start with the first cell as our best guess. Whenever we find a bigger value, we save it with its row and column. After both loops finish, the saved values point to 99.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Program 14: Count Zeros in a Sparse Matrix<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A sparse matrix has mostly zeros. Counting them tells you whether a special storage format would save memory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    matrix := [][]int{\n        {0, 0, 3, 0},\n        {0, 0, 0, 0},\n        {5, 0, 0, 1},\n    }\n\n    \/\/ Step 1: count all cells and the zero cells.\n    zeros, total := 0, 0\n    for _, row := range matrix {\n        for _, value := range row {\n            total++\n            if value == 0 {\n                zeros++\n            }\n        }\n    }\n\n    \/\/ Step 2: work out the percentage. float64 turns whole numbers into decimals.\n    percent := float64(zeros) \/ float64(total) * 100\n    fmt.Printf(\"Zeros: %d of %d (%.0f%%)\\n\", zeros, total, percent) \/\/ %% prints a % sign\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Zeros: 9 of 12 (75%)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The loops count every cell and every zero. Then we turn both counts into decimals with <code>float64<\/code>, so the division does not drop the fraction. Here 9 out of 12 cells are zero, which is 75 percent.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2D Array vs Slice of Slices in Go Matrix Programs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Go gives you two main ways to store a grid. The diagram and table below compare them.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-2-1024x640.png\" alt=\"Go matrix programs comparison of 2D arrays and slices of slices\" class=\"wp-image-758\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A 2D array has a fixed size in one block of memory, while a slice of slices can be any size at run time.<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>2D array [3][3]int<\/th><th>Slice of slices [][]int<\/th><\/tr><\/thead><tbody><tr><td>Size<\/td><td>Fixed at compile time<\/td><td>Chosen at run time<\/td><\/tr><tr><td>Memory<\/td><td>One block<\/td><td>One block per row<\/td><\/tr><tr><td>Copy on assign<\/td><td>Yes, full copy<\/td><td>No, shares rows<\/td><\/tr><tr><td>Rows can differ in length<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Best for<\/td><td>Boards, small fixed grids<\/td><td>Input data, any size<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, use a 2D array for fixed boards like tic-tac-toe. Use a slice of slices for data whose size you only know when the program runs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes in Go Matrix Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Forgetting to make each row:<\/strong> <code>make([][]int, n)<\/code> only makes the outer slice, so each row starts as nil.<\/li>\n\n\n<li><strong>Mixing up rows and columns:<\/strong> <code>len(m)<\/code> is rows and <code>len(m[0])<\/code> is columns, so check both.<\/li>\n\n\n<li><strong>Sharing one row:<\/strong> reusing the same row slice for every row means all rows change together.<\/li>\n\n\n<li><strong>Wrong sizes for multiplication:<\/strong> the columns of the first must equal the rows of the second.<\/li>\n\n\n<li><strong>Empty matrix:<\/strong> <code>m[0]<\/code> panics when there are no rows, so check <code>len(m) &gt; 0<\/code> first.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Matrix Programs<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Write a newMatrix helper<\/strong>, so every row is created the right way.<\/li>\n\n\n<li><strong>Return errors for size problems<\/strong>, instead of letting the program panic.<\/li>\n\n\n<li><strong>Use range<\/strong> for reading, and indexes only when you need the positions.<\/li>\n\n\n<li><strong>Consider one flat slice<\/strong> with <code>r*cols + c<\/code> for very large grids, since it is faster.<\/li>\n\n\n<li><strong>Draw a small example<\/strong> before you code rotations or spirals.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Go Matrix Programs Interview Questions and Answers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interviewers use short Go matrix programs to test index rules and memory. Each program is complete, with real output from Go 1.27.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Go Matrix Programs Index and Size Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    matrix := [][]int{{1, 2, 3}, {4, 5, 6}}\n    \/\/ rows, columns, row 1 col 0, row 0 col 2\n    fmt.Println(len(matrix), len(matrix[0]), matrix[1][0], matrix[0][2])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>2 3 4 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There are 2 rows and 3 columns. <code>matrix[1][0]<\/code> is row 1, column 0, which holds 4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2. Why does this program panic?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nfunc main() {\n    matrix := make([][]int, 2) \/\/ two rows, but each row is still nil\n    matrix[0][0] = 1           \/\/ row 0 has length 0, so index 0 does not exist\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>panic: runtime error: index out of range [0] with length 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>make([][]int, 2)<\/code> creates two nil rows. Each row has length 0, so index 0 does not exist yet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    row := make([]int, 3)\n    grid := [][]int{row, row} \/\/ both rows are the SAME slice\n    grid[0][0] = 7\n    fmt.Println(grid)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[[7 0 0] [7 0 0]]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both rows point to the same slice. So a change through one row shows in the other, which is a common bug.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q4. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    board := [2][2]int{{1, 2}, {3, 4}}\n    copyBoard := board \/\/ arrays are values, so this copies every cell\n    copyBoard[0][0] = 100\n    fmt.Println(board[0][0], copyBoard[0][0])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>1 100<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A 2D array is a value, so <code>copyBoard := board<\/code> copies every cell. With a slice of slices, the same code would change both.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Algorithm Questions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q5. What is the time complexity of multiplying two n x n matrices?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The simple three-loop method is O(n cubed). Faster methods exist, such as Strassen&#8217;s algorithm, but they are rarely needed in normal Go code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q6. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    size := 4\n    for i := 0; i &lt; size; i++ {\n        for j := 0; j &lt; size; j++ {\n            \/\/ || means \"or\": print X on either diagonal.\n            if i == j || i+j == size-1 {\n                fmt.Print(\"X \")\n            } else {\n                fmt.Print(\". \")\n            }\n        }\n        fmt.Println()\n    }\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>X . . X\n. X X .\n. X X .\nX . . X<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>i == j<\/code> marks the main diagonal, and <code>i + j == size - 1<\/code> marks the other one. Together they draw a big X.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q7. How do you rotate a matrix anticlockwise?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Reverse each row, then transpose. Or transpose first and reverse each column. Draw a 3&#215;3 example to check, because the order of steps matters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q8. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    rows, cols := 2, 3\n    flat := make([]int, rows*cols) \/\/ one slice holds all 6 cells\n    for row := 0; row &lt; rows; row++ {\n        for col := 0; col &lt; cols; col++ {\n            flat[row*cols+col] = row*10 + col \/\/ row*cols+col finds the cell's place\n        }\n    }\n    fmt.Println(flat, flat[1*cols+2])\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>[0 1 2 10 11 12] 12<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A flat slice stores the grid row after row. The cell at row r, column c sits at index <code>r*cols + c<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q9. How do you search a matrix where every row is sorted?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run a binary search on each row, which costs O(rows x log cols). If the columns are also sorted, start at the top right corner and step left or down, which costs O(rows + cols). We wrote this staircase search in our <a href=\"https:\/\/smitabharat.com\/blog\/go-searching-programs\/\">Go searching programs<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q10. What does this program print?<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\nfunc main() {\n    jagged := [][]int{{1}, {2, 3}, {4, 5, 6}} \/\/ rows of different lengths\n    total := 0\n    for _, row := range jagged {\n        total += len(row)\n    }\n    fmt.Println(total, len(jagged[2]))\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>6 3<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Rows of a slice of slices can have different lengths. This shape is called a jagged matrix.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A tic-tac-toe checker is a fun way to practise Go matrix programs. Write a program that checks a 3&#215;3 board stored as <code>[3][3]string<\/code> and prints the winner, or &#8220;draw&#8221; if nobody has won. Hint: check all rows, all columns and both diagonals with the index rules from this post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/tour\/moretypes\/14\">A Tour of Go: Slices of slices<\/a><\/li>\n\n\n<li><a href=\"https:\/\/go.dev\/ref\/spec#Array_types\">Go spec: array types<\/a><\/li>\n\n\n<li><a href=\"https:\/\/pkg.go.dev\/slices\">slices package documentation<\/a><\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go matrix programs use <code>[][]int<\/code> for any size or <code>[N][M]int<\/code> for fixed grids.<\/li>\n\n\n<li><code>len(m)<\/code> gives the rows and <code>len(m[0])<\/code> gives the columns.<\/li>\n\n\n<li>Create every row with <code>make<\/code>, or writes will panic.<\/li>\n\n\n<li>Rotate 90 degrees clockwise by transposing and then reversing each row.<\/li>\n\n\n<li>A 2D array copies on assignment, while a slice of slices shares its rows.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">That completes Level 3 of our Go programs series. Coming soon: Level 4, starting with Go function programs such as variadic functions, multiple returns and closures. Until then, test yourself with the <a href=\"https:\/\/smitabharat.com\/blog\/go-slice-interview-questions-part-5\/\">Go slice interview questions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Go matrix programs work with rows and columns of numbers, like a spreadsheet, a game board or the pixels of an image. In this&hellip;<\/p>\n","protected":false},"author":1,"featured_media":759,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[106],"tags":[96,95,11,116,104],"swapz_series":[],"class_list":["post-782","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-programs","tag-beginner","tag-go-interview-questions","tag-go-lang","tag-go-matrix","tag-go-programs"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Matrix Programs: Add, Multiply and Transpose<\/title>\n<meta name=\"description\" content=\"Go matrix programs with output: addition, multiplication, transpose, diagonals, rotate 90 degrees and spiral order, plus matrix interview questions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Matrix Programs: Add, Multiply and Transpose\" \/>\n<meta property=\"og:description\" content=\"Go matrix programs with output: addition, multiplication, transpose, diagonals, rotate 90 degrees and spiral order, plus matrix interview questions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-07T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-28T19:59:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Matrix Programs: Addition, Multiplication, Transpose, Rotate\",\"datePublished\":\"2026-10-07T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/\"},\"wordCount\":1916,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-matrix-programs-featured.png\",\"keywords\":[\"Beginner\",\"Go Interview Questions\",\"Go Lang\",\"Go Matrix\",\"Go Programs\"],\"articleSection\":[\"Go Programs\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/\",\"name\":\"Go Matrix Programs: Add, Multiply and Transpose\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-matrix-programs-featured.png\",\"datePublished\":\"2026-10-07T03:30:00+00:00\",\"dateModified\":\"2026-11-28T19:59:35+00:00\",\"description\":\"Go matrix programs with output: addition, multiplication, transpose, diagonals, rotate 90 degrees and spiral order, plus matrix interview questions.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-matrix-programs-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-matrix-programs-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Matrix Programs featured image: matrix add, multiply and rotate output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-matrix-programs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Matrix Programs: Addition, Multiplication, Transpose, Rotate\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Matrix Programs: Add, Multiply and Transpose","description":"Go matrix programs with output: addition, multiplication, transpose, diagonals, rotate 90 degrees and spiral order, plus matrix interview questions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/","og_locale":"en_US","og_type":"article","og_title":"Go Matrix Programs: Add, Multiply and Transpose","og_description":"Go matrix programs with output: addition, multiplication, transpose, diagonals, rotate 90 degrees and spiral order, plus matrix interview questions.","og_url":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-10-07T03:30:00+00:00","article_modified_time":"2026-11-28T19:59:35+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"17 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Matrix Programs: Addition, Multiplication, Transpose, Rotate","datePublished":"2026-10-07T03:30:00+00:00","dateModified":"2026-11-28T19:59:35+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/"},"wordCount":1916,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-featured.png","keywords":["Beginner","Go Interview Questions","Go Lang","Go Matrix","Go Programs"],"articleSection":["Go Programs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/","url":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/","name":"Go Matrix Programs: Add, Multiply and Transpose","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-featured.png","datePublished":"2026-10-07T03:30:00+00:00","dateModified":"2026-11-28T19:59:35+00:00","description":"Go matrix programs with output: addition, multiplication, transpose, diagonals, rotate 90 degrees and spiral order, plus matrix interview questions.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-matrix-programs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-matrix-programs-featured.png","width":2400,"height":1260,"caption":"Go Matrix Programs featured image: matrix add, multiply and rotate output"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-matrix-programs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Matrix Programs: Addition, Multiplication, Transpose, Rotate"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=782"}],"version-history":[{"count":1,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/782\/revisions"}],"predecessor-version":[{"id":884,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/782\/revisions\/884"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/759"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=782"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}