[英]Can I make the index int64 in golang's for-range iteration?
不,如果您使用帶有“ range”子句的“ for”語句,則規范將索引的類型指定為int
:
Range expression 1st value 2nd value
array or slice a [n]E, *[n]E, or []E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, <-chan E element e E
您無能為力,也無能為力。 切片/數組的長度將適合int
。
不可能使切片大於max int
。 嘗試使用常量表達式制作更大的片段是編譯時錯誤:
x := make([]struct{}, 3123456789)
編譯時錯誤: len argument too large in make([]struct {})
注意: int
大小是特定於實現的:它是32位或64位。 此處產生錯誤的常量表達式是針對32位int
的(Go Playground使用32位int
)。
如果length是運行時表達式,則將出現以下情況:
i := uint(3123456789)
y := make([]struct{}, i)
運行時錯誤: panic: runtime error: makeslice: len out of range
並且數組類型的長度也必須適合int
: Spec:數組類型:
長度是數組類型的一部分; 它必須計算出一個可以由
int
類型的值表示的非負常量 。
嘗試使用更大的長度是一個編譯時錯誤:
var x [3123456789]struct{}
type t1 [3123456789]byte
type t2 [3123456789]struct{}
所有編譯時錯誤: array bound is too large
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.