Does Go allow functions to add array length constraints to the signature, or would length still require a runtime check?
1条回答 默认 最新
- duanmao1975 2016-11-27 21:43关注
For arrays it is more than possible, it is required. For slices it is impossible.
package main import ( "fmt" ) func main() { d := [2]int{1, 2} fmt.Println(sum(d)) } func sum(data [2]int) int { return data[0] + data[1] }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报