How to find the capacity of Channel, Pointer and Slice in Golang?

Go language, capacity defines the maximum number of elements that a particular can hold. Here the task is to find the capacity of Channel, Pointer, and Slice in Golang, we can use the cap() function.

Syntax:

func cap(l Type) int

Here, the type of l is a pointer.

Let us discuss this concept with the help of the examples:

Example 1: In this example, cap() function is used to find the capacity of Pointer in Golang.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to find the 
// capacity of the pointer 
package main 
    
import ( 
    "fmt"
    
// Main function 
func main() { 
    
    // Creating and initializing 
    // pointer
    // Using var keyword 
    var ptr1 [7]*int
    var ptr2 [5]*string 
    var ptr3 [8]*float64 
    
    // Finding the capacity of 
    // the pointer
    // Using cap function 
    fmt.Println("Capacity of ptr1: ", cap(ptr1)) 
    fmt.Println("Capacity of ptr2: ", cap(ptr2)) 
    fmt.Println("Capacity of ptr3: ", cap(ptr3)) 
    

chevron_right


Output:

Capacity of ptr1:  7
Capacity of ptr2:  5
Capacity of ptr3:  8

Example 2: In this example, cap() function is used to find the capacity of Channel in Golang.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to find the 
// capacity of the Channel
package main 
    
import ( 
    "fmt"
    
// Main function 
func main() { 
    
    // Creating and initializing 
    // Channel
    // Using var keyword 
    ch1 := make(chan int, 7) 
    ch2 := make(chan string, 5)
    ch3 := make(chan float64, 8)
    
    // Finding the capacity of 
    // the Channel
    // Using cap function 
    fmt.Println("Capacity of Channel1: ", cap(ch1)) 
    fmt.Println("Capacity of Channel2: ", cap(ch2)) 
    fmt.Println("Capacity of Channel3: ", cap(ch3)) 
    

chevron_right


Output:

Capacity of Channel1:  7
Capacity of Channel2:  5
Capacity of Channel3:  8

Example 3: In this example, cap() function is used to find the capacity of Slice in Golang.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Go program to illustrate how to find the 
// capacity of the Slice 
package main 
    
import ( 
    "fmt"
    
// Main function 
func main() { 
    
    // Creating and initializing 
    // Slice 
    // Using var keyword 
    sl1 := make([]int, 0, 7) 
    sl2 := make([]string, 0, 5)
    sl3 := make([]float64, 0, 8)
    
    // Finding the capacity of 
    // the Slice 
    // Using cap function 
    fmt.Println("Capacity of Slice1: ", cap(sl1)) 
    fmt.Println("Capacity of Slice2: ", cap(sl2)) 
    fmt.Println("Capacity of Slice3: ", cap(sl3)) 
    
}

chevron_right


Output:

Capacity of Slice1:  7
Capacity of Slice2:  5
Capacity of Slice3:  8



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.