Two Matrix Multiplication in golang

Go program for Two Matrix Multiplication. Here more solutions.
package main
import "fmt"
/*
GoLang program for
Two matrix multiplication
*/
// Display the element of given 2d matrix
func printRecord(matrix[][] int) {
fmt.Println(" --------------")
// Assume N x N Matrix size
var row int = len(matrix)
var col int = len(matrix[0])
// Iterate the row element
for i := 0 ; i < row ; i++ {
// Iterate the column element
for j := 0 ; j < col ; j++ {
// Display element value
fmt.Print(" ", matrix[i][j])
}
// Add new line
fmt.Println()
}
fmt.Println()
}
func multiplication(a, b [][]int) {
// Get the size
var row int = len(a)
var col int = len(a[0])
// This matrix are store the result of multiplication
var result = make([][] int, row)
for i := 0 ;i < row; i++{
result[i] = make([] int, col)
}
for i := 0 ; i < row ; i++ {
for j := 0 ; j < col ; j++ {
// Set the initial value of new matrix element
result[i][j] = 0
for k := 0 ; k < row ; k++ {
// Multiply matrix A [i] row and [k] columns to
// the Matrix B [k] columns and [j] rows.
result[i][j] += a[i][k] * b[k][j]
}
}
}
fmt.Println(" Matrix A")
// Print element of matrix x
printRecord(a)
fmt.Println(" Matrix B")
// Print element of matrix y
printRecord(b)
fmt.Println(" Matrix [(A) x (B)]")
// Display resultant matrix
printRecord(result)
}
func main() {
// Define matrix A
var a = [][] int {
{
1,
2,
3,
}, {
6,
1,
2,
}, {
5,
4,
3,
},
}
// Define matrix B
var b = [][] int {
{
3,
1,
3,
}, {
1,
1,
2,
}, {
2,
2,
3,
},
}
multiplication(a, b)
}
Output
Matrix A
--------------
1 2 3
6 1 2
5 4 3
Matrix B
--------------
3 1 3
1 1 2
2 2 3
Matrix [(A) x (B)]
--------------
11 9 16
23 11 26
25 15 32
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment