MOCKSTACKS
EN
Questions And Answers

More Tutorials








Go Array

In Go, an array is a numbered sequence of elements of a specific length. Arrays in Go are much similar to other programming languages, which for example we might need to store a collection of data of the same type.
In an array, you are allowed to store zero or more than zero elements in it. The elements of the array are indexed by using the [] index operator with their zero-based position, means the index of the first element is array[0] and the index of the last element is array[len(array)-1].

Usage of Go Array



Var array_name[length]Type
or
var array_name[length]Typle{item1, item2, item3, ...itemN}

Example of Go Array



package main
  
import "fmt"
  
func main() {
  
 arr_names:= [4]string{"Jhon", "Mike", "Brandon", "William"}
  
 fmt.Println("Elements of the array:")
  
 for i:= 0; i < 3; i++{
  fmt.Println(arr_names[i])
 }

}

Output

Jhon
Mike
Brandon
William


  • Here we create an array a that will hold exactly 4 Strings. The type of elements and length are both part of the array’s type.
  • We can set a value at an index using the array[index] = value syntax, and get it like array[index]. example arr_names[0]="test"
  • The builtin len returns the length of an array.

Change Elements of an Array



package main
  
import "fmt"
  
func main() {
  
 arr_names:= [4]string{"Jhon", "Mike", "Brandon", "William"}
  
 fmt.Println("Elements of the array:")
 arr_names[0] = "new name";
 fmt.Println(arr_names[0]);
}

Output

new name

Access Elements of an Array



package main
  
import "fmt"
  
func main() {
  
 arr_names:= [4]string{"Jhon", "Mike", "Brandon", "William"}
  
 fmt.Println("Elements of the array:")
 
 fmt.Println(arr_names[0]);
 fmt.Println(arr_names[1]);
}

Output

Jhon
Mike

Find the Length of an Array


The len() function is used to find the length of an array:

package main
  
import "fmt"
  
func main() {
  
 arr_names:= [4]string{"Jhon", "Mike", "Brandon", "William"}
  
 fmt.Println("Elements of the array:")
 
 fmt.Println(len(arr_names));
}

Output

4

Conclusion

In this page (written and validated by ) you learned about Go Array . What's Next? If you are interested in completing Go tutorial, your next topic will be learning about: Go Array Sorting.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.