create, use and destroy a 2d array code example

Example 1: create, use and destroy a 2d array

package main
  
import "fmt"
  
func main() {
    rows := 5
    columns := 10
    slice_of_slices := make([][]int , rows)
    for i := 0; i < rows; i++ {
        slice_of_slices[i] = make([]int, columns)
        for j := 0; j < rows; j++{
            slice_of_slices[i][j] = i * j
        }
    }
    slice_of_slices[3][3] = 999
    fmt.Println("Slice of slices: ", slice_of_slices[3][3])
    // Automatically deallocates when out of scope.
}

Example 2: create, use and destroy a 2d array

PROGRAM Example
    IMPLICIT NONE
    INTEGER :: rows, columns, errcheck
    INTEGER, ALLOCATABLE :: array(:,:)
    
    rows = 5
    columns = 10
    ALLOCATE (array(rows,columns), STAT=errcheck) ! STAT is optional and is used for error checking
    array(3, 3) = 999
    WRITE(*,*) array(3, 3)
    DEALLOCATE (array, STAT=errcheck)
END PROGRAM Example

Tags:

Misc Example