github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume1/section3/matrix/matrix.go (about) 1 // An example of a multidimensional array, a 3x4 matrix. 2 package main 3 4 import ( 5 "fmt" 6 ) 7 8 func main() { 9 10 // Here we create a multi-dimensional array, a 3x4 matrix (3 rows, 4 columns) 11 myMatrix := [3][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}} 12 13 // Let's print a value from a cell in each row of the matrix 14 fmt.Println("Value at [0][0]: ", myMatrix[0][0]) 15 fmt.Println("Value at [1][1]: ", myMatrix[1][1]) 16 fmt.Println("Value at [2][2]: ", myMatrix[2][2]) 17 fmt.Println("\n") 18 19 // First attempt at printing the matrix 20 fmt.Printf("%+v\n", myMatrix) 21 fmt.Println("\n") 22 23 // A better looking matrix output to the screen 24 printThreeByFourMatrix(myMatrix) 25 } 26 27 // A function to print the 3x4 matrix in a more pretty manner 28 func printThreeByFourMatrix(inputMatrix [3][4]int) { 29 30 rowLength := len(inputMatrix) 31 columnLength := len(inputMatrix[0]) 32 33 for i := 0; i < rowLength; i++ { 34 for j := 0; j < columnLength; j++ { 35 fmt.Printf("%5d ", inputMatrix[i][j]) 36 } 37 fmt.Println() 38 } 39 40 }