In this post, we will learn about array in go lang
In my previous tutorial, I have explained the installation of Go in windows. Please check out the below URL for that.
https://beginnersbug.com/install-go-lang-in-windows/
Arrays are basic in all programming languages which is used for data operations. We can have a collection of elements here.
Creating array in Go lang
Similar to other programming languages, we can create arrays with length and without length.
In the below example I created two arrays with indexes of 5 & 3 with the datatype of int and string
package main
// fmt package for user input & output
import "fmt"
// Main Method
func main() {
// Creating an int array with index of 5
var intarr = [5]int{10, 4, 6, 2, 12}
// Creating an string array with index of 3
var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
// Printing arrays
fmt.Println(intarr)
fmt.Println(strarr)
}
Output
[10 4 6 2 12]
[Iron Man Hulk Thor]
Printing index of array
In the below example, we will print the element based on the index value.
Here we are printing element at index intarr(1) & strarr(2)
package main
// fmt package for user input & output
import "fmt"
// Main Method
func main() {
// Creating an int array with index of 5
var intarr = [5]int{10, 4, 6, 2, 12}
// Creating an string array with index of 3
var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
// Printing arrays
fmt.Println(intarr)
fmt.Println(strarr)
// Printing index of an array
fmt.Println(intarr[1])
fmt.Println(strarr[2])
}
Output
[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor
Print length of array
The below snippet will show you to print the length of an array.
len() method is used to print the length of the array
package main
// fmt package for user input & output
import "fmt"
// Main Method
func main() {
// Creating an int array with index of 5
var intarr = [5]int{10, 4, 6, 2, 12}
// Creating an string array with index of 3
var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
// Printing arrays
fmt.Println(intarr)
fmt.Println(strarr)
// Printing index of an array
fmt.Println(intarr[1])
fmt.Println(strarr[2])
fmt.Println("Lenght of int array:", len(intarr))
fmt.Println("Lenght of string array:", len(strarr))
}
Output
[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor
Lenght of int array: 5
Lenght of string array: 3
Iterate array using for loop
Here we are printing each and every element with the help of for loop
package main
// fmt package for user input & output
import "fmt"
// Main Method
func main() {
// Creating an int array with index of 5
var intarr = [5]int{10, 4, 6, 2, 12}
// Creating an string array with index of 3
var strarr = [3]string{"Iron Man", "Hulk", "Thor"}
// Printing arrays
fmt.Println(intarr)
fmt.Println(strarr)
// Printing index of an array
fmt.Println(intarr[1])
fmt.Println(strarr[2])
fmt.Println("Lenght of int array:", len(intarr))
fmt.Println("Lenght of string array:", len(strarr))
// Iterating array using for loop
for i := 1; i < len(intarr); i++ {
fmt.Print(intarr[i], " ")
}
fmt.Println()
// Iterating array using for loop
for i := 0; i < len(strarr); i++ {
fmt.Print(strarr[i], " ")
}
}
Output
[10 4 6 2 12]
[Iron Man Hulk Thor]
4
Thor
Lenght of int array: 5
Lenght of string array: 3
4 6 2 12
Iron Man Hulk Thor
Go lang play
You can play with the array using their playgrounds also https://go.dev/tour/moretypes/6