Thursday, 2 May 2019

Go Language: Get capacity of the slice


‘cap’ function returns the capacity of the underlying array.

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