mirror of
https://github.com/golang/go
synced 2024-09-06 00:19:45 +00:00
spec: describe an edge case of slice-to-array conversions
Converting from nil slice to zero element array is ok, so explicitly describe the behavior in the spec. For #46505 Change-Id: I68f432deb6c21a7549bf7e870185fc62504b37f7 Reviewed-on: https://go-review.googlesource.com/c/go/+/430835 TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Robert Griesemer <gri@google.com> Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
parent
c70fd4b30a
commit
4b58b30778
|
@ -1,6 +1,6 @@
|
|||
<!--{
|
||||
"Title": "The Go Programming Language Specification",
|
||||
"Subtitle": "Version of September 8, 2022",
|
||||
"Subtitle": "Version of September 21, 2022",
|
||||
"Path": "/ref/spec"
|
||||
}-->
|
||||
|
||||
|
@ -5542,10 +5542,10 @@ a <a href="#Run_time_panics">run-time panic</a> occurs.
|
|||
<pre>
|
||||
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)
|
||||
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]
|
||||
|
@ -5553,8 +5553,9 @@ s2 := (*[2]byte)(s) // &s2[0] == &s[0]
|
|||
s4 := (*[4]byte)(s) // panics: len([4]byte) > len(s)
|
||||
|
||||
var t []string
|
||||
t0 := (*[0]string)(t) // t0 == nil
|
||||
t1 := (*[1]string)(t) // panics: len([1]string) > len(t)
|
||||
t0 := [0]string(t) // ok for nil slice t
|
||||
t1 := (*[0]string)(t) // t1 == nil
|
||||
t2 := (*[1]string)(t) // panics: len([1]string) > len(t)
|
||||
|
||||
u := make([]byte, 0)
|
||||
u0 := (*[0]byte)(u) // u0 != nil
|
||||
|
|
Loading…
Reference in a new issue