diff options
author | Josh Bleecher Snyder <josharian@gmail.com> | 2020-01-25 11:07:33 -0800 |
---|---|---|
committer | Josh Bleecher Snyder <josharian@gmail.com> | 2021-04-20 23:47:34 +0000 |
commit | 1c268431f49ee2fc843eac52a0854aea3d02a6e0 (patch) | |
tree | c4095e2844e0961d9ecf50f85dc69ea6d9a9c0bd /doc/go_spec.html | |
parent | e12b0afa5454c7683cb27bef0b6979f964dd0e96 (diff) | |
download | go-1c268431f49ee2fc843eac52a0854aea3d02a6e0.tar.gz go-1c268431f49ee2fc843eac52a0854aea3d02a6e0.zip |
spec: allow conversion from slice to array ptr
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>
Diffstat (limited to 'doc/go_spec.html')
-rw-r--r-- | doc/go_spec.html | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/doc/go_spec.html b/doc/go_spec.html index 8c9003434c..13b8beb06c 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -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" }--> @@ -4164,6 +4164,10 @@ in any of these cases: <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> @@ -4314,6 +4318,24 @@ MyRunes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4} </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) // &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(s) +</pre> <h3 id="Constant_expressions">Constant expressions</h3> |