Array
package main
import "fmt"
func main() {
// Declare an array 'a' of integers with a length of 5
var a [5]int
fmt.Println("emp:", a) // Print the content of array 'a' (initialized with zeros)
// Set the fifth element of array 'a' to 100
a[4] = 100
fmt.Println("set:", a) // Print the modified array 'a'
fmt.Println("get:", a[4]) // Print the value of the fifth element in array 'a'
fmt.Println("len:", len(a)) // Print the length of array 'a'
// Declare and initialize an array 'b' with specific values
b := [5]int{1, 2, 3, 4, 5}
fmt.Println("dcl:", b) // Print the content of array 'b'
// Declare a 2D array 'twoD' with 2 rows and 3 columns
var twoD [2][3]int
// Populate the 2D array 'twoD' with values based on the sum of row and column indices
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD) // Print the 2D array 'twoD'
}Output
More Explanation
Last updated