dpd20130 2015-04-19 15:47
浏览 14
已采纳

通道上的cap()不是常数?

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

is function call cap(ch) constant or evaluated?

golang spec said:

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.

seems that is evaluated.

  • 写回答

2条回答 默认 最新

  • dsfs21312 2015-04-19 16:02
    关注

    Yes, cap() is not constant when applied to a channel, as the capacity of a channel isn't encoded in the type and thus not known at compile time.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?