Standard library / reflect

reflect.NewAt()

Creates a pointer Value for existing correctly typed, aligned storage. Caller must ensure storage size, lifetime and validity; prefer ordinary New or ValueOf for normal use. Expected stdout: '9\n'.

Formal Syntax & Structure

go syntaxgo
func NewAt(typ Type, p unsafe.Pointer) Value

Parameters & Attributes

NameTypeDescription
typTypeRuntime type descriptor for the new value.
punsafe.PointerPointer to live, correctly aligned storage matching the element type.

Return Value

Value

Creates a pointer Value for existing correctly typed, aligned storage.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect"  "unsafe")
func main() {  x := 4  v := reflect.NewAt(reflect.TypeFor[int](), unsafe.Pointer(&x))  v.Elem().SetInt(9)  fmt.Println(x)}
Go Reference