Documentation
¶
Index ¶
- func Alignof[ArbitraryType _ArbitraryType](x ArbitraryType) uintptr
- func F[F interface{ ... }](f F, typ ...Type) field
- func Field[F interface{ ... }](f F, typ ...Type) field
- func FindPointerFields(t reflect.Type) [][2]uintptr
- func Malloc(size uintptr, zerofill bool, zero ...[][2]uintptr) unsafe.Pointer
- func New[T any, PT interface{ ... }]() *T
- func NewZero[T any]() *T
- func Sizeof[ArbitraryType _ArbitraryType](x ArbitraryType) uintptr
- func Slice[ArbitraryType _ArbitraryType, IntegerType _IntegerType](ptr *ArbitraryType, len IntegerType) []ArbitraryType
- func SliceData[ArbitraryType _ArbitraryType](slice []ArbitraryType) *ArbitraryType
- func String[IntegerType _IntegerType](ptr *byte, len IntegerType) string
- func StringData(str string) *byte
- func Value[T any](strct any, f field) T
- type Pointer
- type Type
- type UnsafePointerFields
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Field ¶
Field selects a field in a struct. The field can be selected by passing an integer or a (name) string. An optional type constraint can be provided as a safety precaution to ensure that the field's type is what you expected.
func FindPointerFields ¶ added in v1.2.0
FindPointerFields finds the offsets (and corresponding sizes) of all fields in a struct that contain pointers and other reference types.
func Malloc ¶ added in v1.2.0
Malloc allocates contiguous blocks of memory without zeroing. However, the memory will be cleared when zerofill is set.
All allocated memory must sensibly be filled by the developer immediately, since the pre-existing data may contain garbage.
WARNING: If the returned pointer will be cast to a struct with fields containing pointers, then data stored in those specific fields must be cleared before casting.
See: https://github.com/golang/go/issues/76352#issuecomment-3549768452
Note: Reference types (eg. strings, slices, interfaces, channels, maps etc.) contain pointers.
Example:
type Person struct {
name string
age int
phone *int
}
ptr := Malloc(unsafe.Sizeof(Person{}), false, FindPointerFields(reflect.TypeFor[Person]()))
p := *(*Person)(ptr)
func New ¶ added in v1.2.0
func New[T any, PT interface { *T UnsafePointerFields }]() *T
New allocates memory for a struct without zeroing. A pointer to the newly allocated value is returned. The returned struct is not guaranteed to be the zero value.
NOTE: UnsafePointerFields must be implemented as a method with a pointer receiver. UnsafePointerFields is used to selectively zero fields with pointers or reference types.
Example:
var pfs = FindPointerFields(reflect.TypeFor[Person]())
type Person struct {
name string
age int
phone *int
}
func (p *Person) UnsafePointerFields() [][2]uintptr {
return pfs
return [][2]uintptr {
{unsafe.Offsetof(Person{}.name), unsafe.Sizeof(Person{}.name)},
{unsafe.Offsetof(Person{}.phone), unsafe.Sizeof(Person{}.phone)},
}
return nil
}
func main() {
p := New[Person]() // like p := new(Person)
}
func NewZero ¶ added in v1.2.2
func NewZero[T any]() *T
NewZero allocates memory for a zero-value struct. A pointer to the newly allocated value is returned. It is equivalent to new(T) (but seems to be faster).
func Slice ¶
func Slice[ArbitraryType _ArbitraryType, IntegerType _IntegerType](ptr *ArbitraryType, len IntegerType) []ArbitraryType
Slice
func SliceData ¶
func SliceData[ArbitraryType _ArbitraryType](slice []ArbitraryType) *ArbitraryType
SliceData
func StringData ¶
StringData
Types ¶
type Pointer ¶
Pointer
type Type ¶ added in v1.1.2
Type
type UnsafePointerFields ¶ added in v1.3.0
type UnsafePointerFields interface {
// UnsafePointerFields must be implemented on the pointer type *T.
// The return values can be hardcoded using unsafe.Offsetof() and unsafe.Sizeof().
// Alternatively, the automatic FindPointerFields function can be stored in a global
// variable and subsequently returned.
//
// NOTE: When hardcoding, special attention is needed for fields of type Array with elements
// containing pointers (including structs which itself contains pointer fields).
UnsafePointerFields() [][2]uintptr
}
UnsafePointerFields returns the offset and size for each field of a struct that contain pointers.
Note: Reference types (eg. strings, slices, interfaces, channels, maps etc.) contain pointers.