get the nth element of array in Go
Code snippet on how to get the nth element of array in Go
package main
func GetNthElement(arr []string, n int) string {
return arr[n]
}
func main() {
arr := []string{"a", "b", "c"}
println(GetNthElement(arr, 1))
}
This code is an example of how to get the nth element of an array in Go. It starts by declaring a package which is used to group related Go source files. The GetNthElement function is declared which takes an array of type string and an int value as its parameters and returns a string. An array arr is created containing the elements “a”, “b”, and “c”. The GetNthElement function is invoked using the array and the int value 1. Since arrays are zero-based in Go, this will return the second element, “b”, of the array. The results of the function are printed to the console.