SoFunction
Updated on 2025-03-05

Detailed explanation of types and functions in Golang unsafe package

type

It is usually used for type conversion and pointer operation, and is defined as follows:

type Pointer *ArbitraryType

Other types of pointers can be converted to types for low-level operations. See a simple example:

package main
import (
	"fmt"
	"unsafe"
)
func main() {
	i := 30
	ptr1 := &i
	var ptr2 *int64 = (*int64)((ptr1))
	*ptr2 = 8
	(i)
}

type

Definition is as follows:

type ArbitraryType int

ArbitraryType is for document purposes only and is not actually part of the unsafe package, used to represent types of arbitrary Go expressions.

type

Definition is as follows:

type IntegerType int

IntegerType is for documentation purposes only and is not actually part of the unsafe package, used to represent any integer type.

function

Definition is as follows:

func Add(ptr Pointer, len IntegerType) Pointer

Used to perform addition of pointers, add a pointer to the specified offset to obtain a new pointer. A simple example is as follows:

package main
import (
	"fmt"
	"unsafe"
)
func main() {
	arr := []int{1, 2, 3, 4}
	index := 2
	ptr := (&arr[0])
	newPtr := (ptr, uintptr(index)*(arr[0]))
	cc := (*int)(newPtr)
	(*cc)
	(newPtr)
}

function

Definition is as follows:

func Sizeof(x ArbitraryType) uintptr

Used to get the byte size of a type or value. A simple example is as follows:

package main
import (
	"fmt"
	"unsafe"
)
func main() {
	var arr [5]int
	((arr)) // Output: 40	((0))   // Output: 8}

function

Definition is as follows:

func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType

Used to create a slice that shares the underlying data with the original array. A simple example is as follows:

package main
import (
	"fmt"
	"unsafe"
)
func main() {
	var arr [5]int
	arr[0] = 10
	arr[1] = 20
	arr[2] = 30
	arr[3] = 40
	arr[4] = 50
	slice := (&arr[0], 3)
	(slice) // Output: [10 20 30]}

Define an array arr containing 5 integers, and then use the function to create a slice slice starting from index 0 and length 3.

Definition is as follows:

func Offsetof(x ArbitraryType) uintptr

The function is to return the number of bytes at the beginning of the structure member's position in memory from the position of the structure (the offset of the first field of the structure is 0). A simple example is as follows:

package main
import (
	"fmt"
	"unsafe"
)
type MyStruct struct {
	Field1 int64
	Field2 string
}
func main() {
	var myStruct MyStruct
	((myStruct.Field1)) // Output: 0	((myStruct.Field2)) // Output: 8}

Definition is as follows:

func Alignof(x ArbitraryType) uintptr

Returns the number of bytes required for parameter alignment. For some structures, the memory alignment of its fields may affect the memory size occupied by the structure. A simple example is as follows:

package main
import (
	"fmt"
	"unsafe"
)
type MyStruct struct {
	Field1 int
	Field2 string
}
func main() {
	((int(0)))     // Output: 8	((string(""))) // Output: 8	((MyStruct{})) // Output: 8}

summary

The unsafe package provides some low-level operations. You must be cautious when using it and must be very familiar with what you are doing to avoid introducing unsafe issues.

This is the end of this article about the detailed explanation of types and functions in the Golang unsafe package. For more related contents of the Golang unsafe package, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!