⭐ the project to show your appreciation.
This package is for "safely" modifying unexported fields of a struct.
Contrary to popular belief, the unsafe package is actually safe to use - provided you know what
you are doing.
- You need to ensure the
newValueis the same type as the field's type. - You need to make sure the field is not being modifed by multiple go-routines concurrently.
- 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.
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")val := unsafe.Value[string](&e, unsafe.F("e"))- 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