func main() {
s := make([]byte, 8, 10)
+ for i := range s {
+ s[i] = byte(i)
+ }
if p := (*[8]byte)(s); &p[0] != &s[0] {
panic("*[8]byte conversion failed")
}
+ if [8]byte(s) != *(*[8]byte)(s) {
+ panic("[8]byte conversion failed")
+ }
wantPanic(
func() {
_ = (*[9]byte)(s)
},
- "runtime error: cannot convert slice with length 8 to pointer to array with length 9",
+ "runtime error: cannot convert slice with length 8 to array or pointer to array with length 9",
+ )
+ wantPanic(
+ func() {
+ _ = [9]byte(s)
+ },
+ "runtime error: cannot convert slice with length 8 to array or pointer to array with length 9",
)
var n []byte
if p := (*[0]byte)(n); p != nil {
panic("nil slice converted to *[0]byte should be nil")
}
+ _ = [0]byte(n)
z := make([]byte, 0)
if p := (*[0]byte)(z); p == nil {
panic("empty slice converted to *[0]byte should be non-nil")
}
+ _ = [0]byte(z)
+
+ var p *[]byte
+ wantPanic(
+ func() {
+ _ = [0]byte(*p) // evaluating *p should still panic
+ },
+ "runtime error: invalid memory address or nil pointer dereference",
+ )
// Test with named types
type Slice []int