Standard library / unsafe
unsafe.SliceData()
Obtains a pointer to the backing array of a slice. Positive capacity gives a pointer to its first backing element even when length is zero. Nil slice returns nil; a nonnil slice with zero capacity returns a nonnil pointer to an unspecified address, which must not be dereferenced. Signatures use documentation-only placeholders where applicable; these are compiler intrinsics, not ordinary function values. Expected stdout: '[8 5]\ntrue\n'.
Formal Syntax & Structure
go syntaxgo
func SliceData(slice []ArbitraryType) *ArbitraryTypeParameters & Attributes
| Name | Type | Description |
|---|---|---|
| slice | []ArbitraryType | Slice whose backing array is inspected. |
Return Value
*ArbitraryType
Pointer to backing storage under the documented capacity and nil rules.
Working Example
Example Snippetgo
package main
import ( "fmt" "unsafe")
func main() { s := []int{4, 5} p := unsafe.SliceData(s) *p = 8 fmt.Println(s) fmt.Println(unsafe.SliceData([]int(nil)) == nil)}