Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Array In Golang

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 
    

    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 
    

    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
    

The post Array In Golang appeared first on Prwatech.



This post first appeared on Learn Big Data Hadoop In Bangalore, please read the originial post: here

Share the post

Array In Golang

×

Subscribe to Learn Big Data Hadoop In Bangalore

Get updates delivered right to your inbox!

Thank you for your subscription

×