Skip to content

Commit

Permalink
spec: allow conversion from slice to array ptr
Browse files Browse the repository at this point in the history
Implementation follows in subsequent changes.

Updates #395

Change-Id: Ic97ee822805e4c236fdd9d224e776cb2ae62c817
Reviewed-on: https://go-review.googlesource.com/c/go/+/216424
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
  • Loading branch information
josharian committed Apr 20, 2021
1 parent e12b0af commit 1c26843
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion doc/go_spec.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
"Subtitle": "Version of Mar 16, 2021",
"Subtitle": "Version of Apr 20, 2021",
"Path": "/ref/spec"
}-->

Expand Down Expand Up @@ -4164,6 +4164,10 @@ <h3 id="Conversions">Conversions</h3>
<li>
<code>x</code> is a string and <code>T</code> is a slice of bytes or runes.
</li>
<li>
<code>x</code> is a slice, <code>T</code> is a pointer to an array,
and the slice and array types have <a href="#Type_identity">identical</a> element types.
</li>
</ul>

<p>
Expand Down Expand Up @@ -4314,6 +4318,24 @@ <h4 id="Conversions_to_and_from_a_string_type">Conversions to and from a string
</li>
</ol>

<h4 id="Conversions_from_slice_to_array_pointer">Conversions from slice to array pointer</h4>

<p>
Converting a slice to an array pointer yields a pointer to the underlying array of the slice.
If the <a href="#Length_and_capacity">length</a> of the slice is less than the length of the array,
a <a href="#Run_time_panics">run-time panic<a/> occurs.
</p>

<pre>
s := make([]byte, 2, 4)
s0 := (*[0]byte)(s) // s0 != nil
s2 := (*[2]byte)(s) // &amp;s2[0] == &amp;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(s)
</pre>

<h3 id="Constant_expressions">Constant expressions</h3>

Expand Down

0 comments on commit 1c26843

Please sign in to comment.