unsafe

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2025 License: MIT Imports: 2 Imported by: 0

README

⭐   the project to show your appreciation. :arrow_upper_right:

Unsafe

This package is for "safely" modifying unexported fields of a struct.

Safety Considerations

Contrary to popular belief, the unsafe package is actually safe to use - provided you know what you are doing.

  1. You need to ensure the newValue is the same type as the field's type.
  2. You need to make sure the field is not being modifed by multiple go-routines concurrently.
  3. Read Exploring ‘unsafe’ Features in Go 1.20: A Hands-On Demo and Modifying Private Variables of a Struct in Go Using unsafe and reflect.

Example 1 - Setting a field

type Example struct {
	e string // unexported string 
}

Let's modify the Example struct's e field.

import "github.com/rocketlaunchr/unsafe"

e := Example{}

// Option 1
ptr := unsafe.SetField[string](&e, unsafe.F("e"))
*(*string)(ptr) = "New Value" // Note: If the field type is string, then ptr must be cast to *string

// Option 2
unsafe.SetField(&e, unsafe.F("e"), "New Value 2")

Example 2 - Reading a field

val := unsafe.Value[string](&e, unsafe.F("e"))

Other useful packages

  • awesome-svelte - Resources for killing react
  • dataframe-go - Statistics and data manipulation
  • dbq - Zero boilerplate database operations for Go
  • electron-alert - SweetAlert2 for Electron Applications
  • go-pool - A Generic pool
  • igo - A Go transpiler with cool new syntax such as fordefer (defer for for-loops)
  • mysql-go - Properly cancel slow MySQL queries
  • react - Build front end applications using Go
  • remember-go - Cache slow database queries
  • testing-go - Testing framework for unit testing

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alignof

func Alignof[_ArbitraryType ArbitraryType](x _ArbitraryType) uintptr

func F

func F[F interface{ int | []int | string }](f F, typ ...reflect.Type) field

F calls the Field function directly.

func Field

func Field[F interface{ int | []int | string }](f F, typ ...reflect.Type) field

Field selects which field in a struct you wish to modify. The field name can be selected by passing an integer or a string. An optional type constraint can be provided as a safety precaution to ensure that the field's type is what you expected.

func Sizeof

func Sizeof[_ArbitraryType ArbitraryType](x _ArbitraryType) uintptr

func Slice

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

func SliceData

func SliceData[_ArbitraryType ArbitraryType](slice []_ArbitraryType) *_ArbitraryType

func String

func String[_IntegerType IntegerType](ptr *byte, len _IntegerType) string

func StringData

func StringData(str string) *byte

func Value added in v1.0.1

func Value[V any](strct any, f field) V

Value returns the value of the unexported field of a struct.

NOTE: This function panics if strct is not actually a struct or the field could not be found.

Types

type ArbitraryType

type ArbitraryType interface {
	any
}

type IntegerType

type IntegerType interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Pointer

type Pointer = unsafe.Pointer

func Add

func Add[_IntegerType IntegerType](ptr Pointer, len _IntegerType) Pointer

func SetField

func SetField[V any](strct any, f field, newValue ...V) Pointer

SetField allows unexported fields of a struct to be modified. It returns a Pointer to the field if you wish to change the value externally instead of by passing a new value.

NOTE: This function panics if strct is not actually a struct or the field could not be found.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL