Standard library / unsafe

unsafe.Add()

Moves a pointer by a byte offset. The result must remain within the original live allocation and be suitably aligned before typed access. Never advance beyond the object or use nil as an arithmetic base. Use ordinary indexing when possible. Signatures use documentation-only placeholders where applicable; these are compiler intrinsics, not ordinary function values. Expected stdout: '20\n'.

Formal Syntax & Structure

go syntaxgo
func Add(ptr Pointer, len IntegerType) Pointer

Parameters & Attributes

NameTypeDescription
ptrPointerPointer into a live allocation.
lenIntegerTypeByte displacement, representable by int if a constant; resulting pointer must stay inside the allocation.

Return Value

Pointer

Pointer at the displaced byte location.

Working Example

Example Snippetgo
package main
import (  "fmt"  "runtime"  "unsafe")
func main() {  a := [3]int{10, 20, 30}  p := unsafe.Add(unsafe.Pointer(&a[0]), unsafe.Sizeof(a[0]))  fmt.Println(*(*int)(p))  runtime.KeepAlive(&a)}
Go Reference