Golang 如何找到指针的容量
Go编程语言或Golang中的指针是一个变量,用来存储另一个变量的内存地址。Golang中的指针也被称为特殊变量。这些变量用于在系统中的特定内存地址上存储一些数据。内存地址总是以十六进制格式(以0x开始,如0xFFAAF等)。
在指针中,你可以在 cap() 函数的帮助下找到指针的容量。这个函数是一个内置函数,用于返回指向数组的指针的容量。在Go语言中,容量定义了存储在数组指针中的最大元素数。这个函数被定义在内置函数中。
语法
func cap(l Type) int
这里, l 的类型是一个指针。让我们借助于例子来讨论这个概念。
例子
// Go program to illustrate how to find the
// capacity of the pointer to an array
package main
import (
"fmt"
)
// Main function
func main() {
// Creating and initializing
// pointer to array
// Using var keyword
var ptr1 [7]*int
var ptr2 [5]*string
var ptr3 [8]*float64
// Finding the capacity of
// the pointer to array
// Using cap function
fmt.Println("Capacity of ptr1: ", cap(ptr1))
fmt.Println("Capacity of ptr2: ", cap(ptr2))
fmt.Println("Capacity of ptr3: ", cap(ptr3))
}
输出
Capacity of ptr1 : 7
Capacity of ptr2 : 5
Capacity of ptr3 : 8
例2 :
// Go program to illustrate how to find the
// capacity of the pointer to an array
package main
import (
"fmt"
)
// Main function
func main() {
// Creating an array
arr := [8]int{200, 300, 400,
500, 600, 700, 100, 200}
var x int
// Creating pointer
var p [5]*int
// Assigning the address
for x = 0; x < len(p); x++ {
p[x] = &arr[x]
}
// Displaying result
for x = 0; x < len(p); x++ {
fmt.Printf("Value of p[%d] = %d\n",
x, *p[x])
}
// Finding capacity
// using cap() function
fmt.Println("Capacity of arr: ", cap(arr))
fmt.Println("Capacity of p: ", cap(p))
}
输出
Value of p[0] = 200
Value of p[1] = 300
Value of p[2] = 400
Value of p[3] = 500
Value of p[4] = 600
Capacity of arr: 8
Capacity of p: 5
极客教程