是否有可能在常量上下文中使用函數(shù)的參數(shù)?例如func example(size int) {
one := [size]int{} // Error: non-constant array bound 'size'
const two = size // Error: const initializer 'size' is not a constant
}在這些情況下不是有效常數(shù)size嗎?如果不是,為什么?
1 回答
慕慕森
TA貢獻1856條經(jīng)驗 獲得超17個贊
不,這在 Go 中是不可能的。Go 常量是編譯時構造的,而參數(shù)值僅在運行時存在。
規(guī)范:常量表達式:
常量表達式可能只包含常量操作數(shù),并在編譯時求值。
- 1 回答
- 0 關注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消
