gpt4 book ai didi

go - channel 上的 cap() 不是常量?

转载 作者:IT王子 更新时间:2023-10-29 01:33:35 31 4
gpt4 key购买 nike

ch := make(chan int, 10)
fmt.Println(cap(ch))

函数调用 cap(ch) 常量 还是求值

golang 规范说:

The expression len(s) is constant if s is a string constant. The expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls; in this case s is not evaluated. Otherwise, invocations of len and cap are not constant and s is evaluated.

似乎已评估

最佳答案

是的,cap() 在应用于 channel 时不是常量,因为 channel 的容量未编码为类型,因此在编译时未知。

关于go - channel 上的 cap() 不是常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29732049/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com