golang tutorial - Golang Array | Go arrays - golang - go programming language - google go - go language - go program - google language



Golang Array

  • In Go, an array is a numbered sequence of elements of a specific length.
  • In programming, one of the frequently arising problem is to handle numerous data of same type.
  • Consider this situation, you are taking a survey of 100 people and you have to store their age. To solve this problem in GO, you can create an integer array having 100 elements.
  • An array is a collection of data that holds fixed number of values of same type

example:

Var age[100] int;
click below button to copy the code. By - golang tutorial - team
  • Here, the age array can hold maximum of 100 elements of integer type.
  • The size and type of arrays cannot be changed after its declaration
 go array
golang , gopro , google go , golang tutorial , google language , go language , go programming language

General Idea - Elements of an Array and How to access them

  • You can access elements of an array by using indices.
  • Suppose you declared an array mark as above. The first element is mark[0], second element is mark[1] and so on.
 go array acess
 if statement

Few key notes

  • Arrays have 0 as the first index not 1. In this example, mark[0] is the first element.
  • If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4] is the last element.
  • If the array is floating value. If the starting address of mark[0] is 2120d.
    • Then, the next address, a[1], will be 2124d,
    • address of a[2] will be 2128d and so on.
    • It's because the size of a float is 4 bytes.
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Declaring Arrays in Go Language Programming

  • To declare an array in Go, a programmer specifies the type of the elements and the number of elements required by an array. Below is the sample,
var variable_name [SIZE] variable_type
click below button to copy the code. By - golang tutorial - team
  • It’s called as single-dimensional array.
  • The arraySize must be an integer constant greater than zero and type can be any valid Go data type.

Initializing Arrays in Go Language Programming

  • We can initialize array in Go either one by one or using a single continuous statement as below:
var salary  = [5] float32{500.0, 4.0, 5.2, 9.0, 150.0}
click below button to copy the code. By - golang tutorial - team
  • The number of values between braces { } cannot be larger than the number of elements that we declare as the size of the array between square brackets [ ].
  • For dynamic size of the array, we can omit the size of the array. So that, the array will accommodate the dynamic contents and initialize itself based on the number of variables given
var salary  = []float32{500.0, 4.0, 5.2, 9.0, 150.0}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Accessing Array Elements in Go Language Programming

  • An element can be accessed through index values of the array element. This can be achieved by placing the index of the element within square brackets after the name of the array.

example

float32 value = salary [8]
click below button to copy the code. By - golang tutorial - team
  • The above statement will take 9th element from the array and assign the value to salary variable.

Go Program for arrays

package main

	import "fmt"

	func main() {
//// Here we create an array a that will hold exactly 5 ints. The type of elements and length are both part of the array’s type. By default an array is zero-valued, which for ints means 0s.
	    var a [5]int
    fmt.Println("emp:", a)
// We can set a value at an index using the array[index] = value syntax, and get a value with array[index].
	    a[4] = 100
    fmt.Println("set:", a)
    fmt.Println("get:", a[4])
// The builtin len returns the length of an array.
	    fmt.Println("len:", len(a))
// Use this syntax to declare and initialize an array in one line.
	    b := [5]int{1, 2, 3, 4, 5}
    fmt.Println("dcl:", b)
// Array types are one-dimensional, but you can compose types to build multi-dimensional data structures.
	    var twoD [2][3]int
    for i := 0; i < 2; i++ {
        for j := 0; j < 3; j++ {
            twoD[i][j] = i + j
        }
    }
    fmt.Println("2d: ", twoD)
}
click below button to copy the code. By - golang tutorial - team
golang , gopro , google go , golang tutorial , google language , go language , go programming language

Output for the above go program

emp: [0 0 0 0 0]
set: [0 0 0 0 100]
get: 100
len: 5
dcl: [1 2 3 4 5]
2d:  [[0 1 2] [1 2 3]]

Related Searches to Go arrays