Standard library / unsafe

unsafe.Slice()

Creates a slice view starting at a typed pointer, with length and capacity equal to the requested count. Storage is shared, not copied. The full range must fit in live, correctly typed storage. Nil with zero length returns nil; a negative runtime length or nil with nonzero length panics. Signatures use documentation-only placeholders where applicable; these are compiler intrinsics, not ordinary function values. Expected stdout: '[1 9 3]\n3 3\n'.

Formal Syntax & Structure

go syntaxgo
func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType

Parameters & Attributes

NameTypeDescription
ptr*ArbitraryTypePointer to the first element of sufficient live storage.
lenIntegerTypeNonnegative element count; must fit int and the original allocation.

Return Value

[]ArbitraryType

Aliasing slice with length and capacity equal to len.

Working Example

Example Snippetgo
package main
import (  "fmt"  "unsafe")
func main() {  a := [3]int{1, 2, 3}  v := unsafe.Slice(&a[0], len(a))  v[1] = 9  fmt.Println(a)  fmt.Println(len(v), cap(v))}
Go Reference