From: Robert Griesemer Date: Wed, 7 Sep 2022 21:32:22 +0000 (-0700) Subject: spec: describe slice-to-array conversions X-Git-Tag: go1.20rc1~1103 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=2c3187cd4295605033740ff0522b1457a702d84d;p=gostls13.git spec: describe slice-to-array conversions For #46505. Change-Id: I1a30fd895496befd16626afb48717ac837ed5778 Reviewed-on: https://go-review.googlesource.com/c/go/+/429315 Reviewed-by: Matthew Dempsky Reviewed-by: Ian Lance Taylor Reviewed-by: Robert Griesemer --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 29109b6b9e..764dcd2f78 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -5530,16 +5530,23 @@ runes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4} -

Conversions from slice to array pointer

+

Conversions from slice to array or array pointer

-Converting a slice to an array pointer yields a pointer to the underlying array of the slice. -If the length of the slice is less than the length of the array, +Converting a slice to an array yields an array containing the elements of the underlying array of the slice. +Similarly, converting a slice to an array pointer yields a pointer to the underlying array of the slice. +In both cases, if the length of the slice is less than the length of the array, a run-time panic occurs.

 s := make([]byte, 2, 4)
+
+a0 := ([0]byte)(s)
+a1 := ([1]byte)(s[1:])   // a1[0] == s[1]
+a2 := ([2]byte)(s)       // a2[0] == s[0]
+a4 := ([4]byte)(s)       // panics: len([4]byte) > len(s)
+
 s0 := (*[0]byte)(s)      // s0 != nil
 s1 := (*[1]byte)(s[1:])  // &s1[0] == &s[1]
 s2 := (*[2]byte)(s)      // &s2[0] == &s[0]