unsafe

package
v0.0.0-...-90c9d3a Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2010 License: BSD-3-Clause, GooglePatentClause Imports: 0 Imported by: 0

Documentation

Overview

The unsafe package contains operations that step around the type safety of Go programs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alignof

func Alignof(v ArbitraryType) int

Alignof returns the alignment of the value v. It is the maximum value m such that the address of a variable with the type of v will always always be zero mod m. If v is of the form obj.f, it returns the alignment of field f within struct object obj.

func Offsetof

func Offsetof(v ArbitraryType) int

Offsetof returns the offset within the struct of the field represented by v, which must be of the form struct_value.field. In other words, it returns the number of bytes between the start of the struct and the start of the field.

func Reflect

func Reflect(i interface{}) (typ interface{}, addr uintptr)

Reflect unpacks an interface value into its type and the address of a copy of the internal value.

func Sizeof

func Sizeof(v ArbitraryType) int

Sizeof returns the size in bytes occupied by the value v. The size is that of the "top level" of the value only. For instance, if v is a slice, it returns the size of the slice descriptor, not the size of the memory referenced by the slice.

func Typeof

func Typeof(i interface{}) (typ interface{})

Typeof returns the type of an interface value, a runtime.Type.

func Unreflect

func Unreflect(typ interface{}, addr uintptr) (ret interface{})

Unreflect inverts Reflect: Given a type and a pointer, it returns an empty interface value with those contents. The typ is assumed to contain a pointer to a runtime type; the type information in the interface{} is ignored, so that, for example, both *reflect.StructType and *runtime.StructType can be passed for typ.

Types

type ArbitraryType

type ArbitraryType int

ArbitraryType is here for the purposes of documentation only and is not actually part of the unsafe package. It represents the type of an arbitrary Go expression.

type Pointer

type Pointer *ArbitraryType

Pointer represents a pointer to an arbitrary type. There are three special operations available for type Pointer that are not available for other types.

  1. A pointer value of any type can be converted to a Pointer.
  2. A uintptr can be converted to a Pointer.
  3. A Pointer can be converted to a uintptr.

Pointer therefore allows a program to defeat the type system and read and write arbitrary memory. It should be used with extreme care.

func New

func New(typ interface{}) Pointer

New allocates and returns a pointer to memory for a new value of the given type. The typ is assumed to hold a pointer to a runtime type. Callers should use reflect.MakeZero instead of invoking unsafe.New directly.

func NewArray

func NewArray(typ interface{}, n int) Pointer

NewArray allocates and returns a pointer to an array of n elements of the given type. The typ is assumed to hold a pointer to a runtime type. Callers should use reflect.MakeSlice instead of invoking unsafe.NewArray directly.

Jump to

Keyboard shortcuts

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