Array In Golang

  • date 24th August, 2022 |
  • by Prwatech |
  • 0 Comments

Understanding Arrays and Slices in Go

  Arrays and slices are fundamental data structures in the Go programming language for managing collections of elements. An array in Go is a fixed-size sequence of elements of the same type. The size of an array is specified at compile time and cannot be changed at runtime. Arrays provide direct access to individual elements using zero-based indexing. Slices, on the other hand, are dynamic and flexible wrappers around arrays. A slice is a lightweight data structure that represents a segment of an underlying array. Unlike arrays, slices can grow or shrink dynamically at runtime. Slices include a pointer to the underlying array, along with a length and capacity, allowing for efficient operations like appending elements. Understanding the differences between arrays and slices is crucial for effective memory management and data manipulation in Go. Arrays are suitable for fixed-size collections, while slices are preferred for handling variable-size data and supporting dynamic operations. Both arrays and slices are integral to Go's idiomatic approach to managing collections and data structures efficiently. In an array, store zero or more than zero elements in it. which means the index of the first element is array [0] and the index of the last element is an array [len(array)-1].

Arrays are created in two different ways:

  1. Using var keyword
 
  1. Go program to illustrate how to create:
    package main
    import "fmt"
    
    func main() {
        var a [5]int
        fmt.Println(" size of the array: ", a)
    }
    
    Output :
    PS C:\GO_Language\Array> go run arr.go
    thr size of the array [0 0 0 0 0]
    
  2. Go program to illustrate the values of an array
    package main
    
    import "fmt"
    
    func main() {
        a: = [5]int {1, 3, 4, 2, 5}
        sum: = 0
        for i: = 0; i < len(a); i++ {
            sum += a[i]
            fmt.Println("The value :", sum)
        }
    }
    
    Output:
    PS C:\GO_Language\Array> go run arr2.go
    The value : 1
    The value : 4
    The value : 8
    The value : 10
    The value : 15
    
  3. Go program to illustrate and insert the values in an array using for loop
    package main
    
    import "fmt"
    
    func main() {
        var n [10]int
        var i, j int
        for i = 0; i < 10; i++ {
            n[i] = i + 100
        }
        for j = 0; j < 10; j++ {
            fmt.Printf("Element[%d] =%d\n", j, n[j])
        }
    }
    
    Output :
    PS C:\GO_Language\Array> go run arr3.go
    Element[0] =100
    Element[1] =101
    Element[2] =102
    Element[3] =103
    Element[4] =104
    Element[5] =105
    Element[6] =106
    Element[7] =107
    Element[8] =108
    Element[9] =109
    
  4. Go program to illustrate the Address of a variable
    package main
    import "fmt"
    
    func main() {
        x: = 7
        y: = &x
        fmt.Println(x, y)
    
    }
    
    Output:
    PS C:\GO_Language\Array> go run arr4.go
    7 0xc000014088
    
  5. Go program to illustrate Assigning values through pointers
    package main
    
    import "fmt"
    func main() {
        i, j: = 42, 2372
        p: = &i
        fmt.Println(*p)
        *p = 21
        fmt.Println(i)
        p = &j
        *p = *p / 37
        fmt.Println(j)
    
    }
    
    Output:
    PS C:\GO_Language\Array> go run arr5.go
    42
    21
    64
    PS C:\GO_Language\Array>
    
  6. Go program to illustrate the value of null pointer.
    package main
    
    import "fmt"
    
    func main() {
        var ptr *int
        fmt.Printf("the Value of ptr is :%x\n", ptr)
    }
    
    Output :
    PS C:\GO_Language\Array> go run arr6.go
    the Value of ptr is :0
     
    
  7. Go program to illustrate the division using pointer
    package main
    
    import "fmt"
    
    func main() {
        i, j: = 42, 2372
        p: = &i
        fmt.Println(*p)
        fmt.Printf("%T \n ", p)
    
        *p = 21
        fmt.Println(i)
        p = &j
        *p = *p / 37
        fmt.Println(j)
    
    }
    
    Output :
    PS C:\GO_Language\Array> go run arr7.go
    42
    *int 
     21
    64
    
     

Quick Support

image image