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 Malloc(size uintptr, zero bool, t ...reflect.Type) unsafe.Pointer
- func New[T any](fp ...[]PtrOs) *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 PtrOs
- type Type
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 Malloc ¶ added in v1.2.0
Malloc allocates contiguous blocks of memory without zeroing the values. All allocated memory must immediately be filled by the developer, since the pre-existing data may contain garbage.
Warning: If the returned pointer will be cast to a struct with fields that contain pointers, then provide a reflect Type for that struct. See: https://github.com/golang/go/issues/76352#issuecomment-3549768452
Example:
type Person struct {
name string
age int
phone *int
}
ptr := Malloc(unsafe.Sizeof(Person{}), false, reflect.TypeFor[Person]())
p := *(*Person)(ptr)
func New ¶ added in v1.2.0
New allocates memory for a struct. The value returned is a pointer to a newly allocated value of that type. The returned struct is not guaranteed to be the zero value.
Example:
var pts = FindPointerFields(reflect.TypeFor[Person]())
type Person struct {
name string
age int
phone *int
}
func main() {
p := New[Person](pts) // like p := new(Person)
}
func NewZero ¶ added in v1.2.2
func NewZero[T any]() *T
NewZero allocates memory for a zero-value struct. The value returned is a pointer to a newly allocated value of that type. It is equivalent to new(T) (but appears 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 PtrOs ¶ added in v1.2.1
type PtrOs struct {
// contains filtered or unexported fields
}
PtrOs represents the offsets of a struct's Pointer fields.
func FindPointerFields ¶ added in v1.2.0
FindPointerFields finds the offsets of all fields in a struct that contain pointers and other reference types.