在 Go 语言中,容量运算符(capacity operator)并不是一个单独的运算符,而是通过内置函数 cap 来获取集合类型(如切片、数组、通道)的容量。

1. 容量(Capacity)的概念

  • 容量指的是集合类型在不重新分配内存的情况下,能够存储的最大元素数量。对于切片和数组,容量是从切片或数组的起始位置到其底层数组的末尾的长度。

2. 使用 cap 函数获取容量

  • 切片(slice)cap 函数返回切片的容量,即底层数组从切片的起始位置到数组末尾的长度。
  • 数组(array)cap 函数返回数组的长度和容量,这两个值是相同的。
  • 通道(channel)cap 函数返回通道的缓冲区容量,即通道可以缓存的最大元素数量。

示例代码

切片的容量

package main

import "fmt"

func main() {
    s := make([]int, 5, 10) // 创建一个长度为5、容量为10的切片
    fmt.Println("Slice:", s)
    fmt.Println("Length:", len(s))
    fmt.Println("Capacity:", cap(s)) // 输出: Capacity: 10
}

在这个示例中,cap(s) 返回 10,因为切片 s 的容量为 10。

数组的容量

package main

import "fmt"

func main() {
    a := [5]int{1, 2, 3, 4, 5}
    fmt.Println("Array:", a)
    fmt.Println("Length:", len(a))
    fmt.Println("Capacity:", cap(a)) // 输出: Capacity: 5
}

在这个示例中,cap(a) 返回 5,因为数组 a 的长度和容量都是 5。

通道的容量

package main

import "fmt"

func main() {
    c := make(chan int, 5) // 创建一个缓冲区容量为5的通道
    fmt.Println("Channel capacity:", cap(c)) // 输出: Channel capacity: 5
}

在这个示例中,cap(c) 返回 5,因为通道 c 的缓冲区容量为 5。

总结

  • Capacity is the maximum number of elements that can be stored in a collection without reallocating memory.
  • Go uses the cap function to get the capacity of slices, arrays, and channels:
    • Slices: cap(slice) returns the capacity of the slice.
    • Arrays: cap(array) returns the length and capacity of the array (same values).
    • Channels: cap(channel) returns the buffer capacity of the channel.

These methods help manage and understand memory usage and performance in Go applications.