From 1c268431f49ee2fc843eac52a0854aea3d02a6e0 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Sat, 25 Jan 2020 11:07:33 -0800 Subject: [PATCH] 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 Run-TryBot: Josh Bleecher Snyder Reviewed-by: Matthew Dempsky Reviewed-by: Robert Griesemer Reviewed-by: Rob Pike --- doc/go_spec.html | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 8c9003434c2..13b8beb06c9 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -4164,6 +4164,10 @@ in any of these cases:
  • x is a string and T is a slice of bytes or runes.
  • +
  • + x is a slice, T is a pointer to an array, + and the slice and array types have identical element types. +
  • @@ -4314,6 +4318,24 @@ MyRunes("白鵬翔") // []rune{0x767d, 0x9d6c, 0x7fd4} +

    Conversions from slice to 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, +a run-time panic occurs. +

    + +
    +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)
    +

    Constant expressions