Standard library / reflect

reflect.SliceAt()

Creates a slice view over existing storage at a pointer, with length and capacity n. Caller must ensure alignment, lifetime and enough contiguous elements; this is not a bounds-checking allocator. Expected stdout: '[1 8 3]\n'.

Formal Syntax & Structure

go syntaxgo
func SliceAt(typ Type, p unsafe.Pointer, n int) Value

Parameters & Attributes

NameTypeDescription
typTypeRuntime type descriptor for the new value.
punsafe.PointerPointer to live, correctly aligned storage matching the element type.
nintRequested size, length, capacity or growth amount, as specified by the operation.

Return Value

Value

Creates a slice view over existing storage at a pointer, with length and capacity n.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect"  "unsafe")
func main() {  a := [3]int{1, 2, 3}  v := reflect.SliceAt(reflect.TypeFor[int](), unsafe.Pointer(&a[0]), len(a))  v.Index(1).SetInt(8)  fmt.Println(a)}
Go Reference