Standard library / reflect

reflect.Value.SetPointer()

Sets a settable unsafe.Pointer Value to a supplied pointer. It does not change the pointed-to value; use correctly typed live storage and avoid invalid pointer conversion. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (v Value) SetPointer(x unsafe.Pointer)

Parameters & Attributes

NameTypeDescription
xunsafe.PointerNew value; must satisfy the destination kind or assignability requirement.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect"  "unsafe")
func main() {  x := 1  var p unsafe.Pointer  reflect.ValueOf(&p).Elem().SetPointer(unsafe.Pointer(&x))  fmt.Println(p == unsafe.Pointer(&x))}
Go Reference