Thumbnail article
Array in Golang

An array is a collection of the same data type and is stored in a variable. The number of elements/data stored in an array cannot exceed the allocated number because the array has a capacity and its value is set at creation time. The default value for each array element initially depends on its data type. For int, each zero value element is 0, for bool, then false, and so on. Each array element has an index in the form of a number representing its position in the order of elements, the index starts from 0.

  • Filling Array Elements that Exceeds Initial Allocation Filling an array element at an index that doesn’t match the allocation will result in an error. As a simple example, if the array has 4 slots, then filling 5 slot values is not allowed. The solution to this problem is to use append keyword.
  • Initialize Initial Value Array Array elements can be populated when declaring variables. To do this, write the element data in curly brackets after the data type and use a comma (,) separator between items.Using the fmt.Println() function on an array data without accessing a specific index produces output in the form of a string of all existing arrays. This technique is usually used for debugging array data.
  • Initialize Array Values with Vertical Style Array elements can be written horizontally or vertically. Especially for vertical array declarations, a comma must be written after the elements, including the last element. If not, an error will appear.
  • Initialize Array Value without Number of Elements An array declaration whose value is specified at the beginning, may not be written in the total width of the array, just replace it with three dots (…). The number of elements is automatically calculated according to the entered element data.
  • Multidimensional Array A multidimensional array is an array where each element is also an array (depending on the dimension depth). The declaration of a multidimensional array is the same as a regular array, namely by writing the data of the next dimension array into the elements of the previous dimension array. For arrays that are sub dimensions or elements, you cannot write the number of data.
  • Looping Array Elements using Keyword for for and array keywords are closely related. You can loop using this keyword, to get array elements. There are several ways to loop a data array, the first is to use iteration variables to access elements based on their indexes.
  • Looping Array Elements using Keyword for – range There is an easier way to loop data arrays, by using the for – range keyword. Sometimes when looping using a for – range, there is a possibility that the data needed is only the element, the index is not. Go does not allow empty or unused variables. If forced, an error will occur. You can use an underscore (_) to contain values you don’t want to use. If you only need the index of the element, you can use 1 variable after the for keyword.
  • Allocation of Array Elements using Keyword make The declaration and allocation of array data can also be done using the make keyword. The first parameter of the make keyword is filled with the desired data type of the array element, and the second parameter is filled with the number of elements.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *