Standard library / unsafe

unsafe.Pointer

A special pointer type that bypasses normal type restrictions. A typed pointer can be converted to Pointer and back to its original type. Reinterpretation requires compatible layout, alignment and sufficient live storage. Converting to uintptr produces an integer, not a GC reference; saving an address and later converting it back is not generally valid. Prefer typed pointers and ordinary indexing. The declaration uses a documentation-only placeholder. Expected stdout: '9\n'.

Formal Syntax & Structure

go syntaxgo
type Pointer *ArbitraryType

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime"  "unsafe")
func main() {  n := 7  p := unsafe.Pointer(&n)  q := (*int)(p)  *q = 9  fmt.Println(n)  runtime.KeepAlive(&n)}
Go Reference