Thursday 2 May 2019

Go language: Get number of elements in the slice


‘len’ function returns number of elements in the slice.

App.go
package main

import "fmt"

func main() {
    data := make([]int, 5, 20)

    fmt.Println("Length : ", len(data))
    fmt.Println("Capacity : ", cap(data))
    fmt.Println("Data : ", data)
}

Output
Length :  5
Capacity :  20
[0 0 0 0 0]



Previous                                                 Next                                                 Home

No comments:

Post a Comment