unsafes

package
v0.5.42 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 9 Imported by: 0

README

サンプルリスト

このディレクトリには以下のサンプルがあります。

file example name note
unsafe_string.go unsafe_string unsafe.String() のサンプルです.
unsafe_stringdata.go unsafe_stringdata unsafe.StringData() のサンプルです.
unsafe_pointer_cast.go unsafe_pointer_cast unsafeパッケージを用いてポインタを任意の型にキャストするサンプルです
unsafe_add.go unsafe_add unsafe.Add関数を利用してポインタ演算するサンプルです
unsafe_slice.go unsafe_slice unsafe.SliceData() と unsafe.Slice() のサンプルです
unsafe_sizeof.go unsafe_sizeof unsafe.Sizeof() についてのサンプルです.
unsafe_offsetof.go unsafe_offsetof unsafe.Offsetof() のサンプルです
unsafe_dump.go unsafe_dump unsafeパッケージを使って構造体のメモリダンプを出力するサンプルです
unsafe_alignof.go unsafe_alignof unsafe.Alignof() のサンプルです

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.5.42

func Add() error

Add は、unsafe.Add関数を利用してポインタ演算するサンプルです。

unsafe.Addは、Go 1.17で追加された関数。 それまでは unsafe.Pointer(uintptr(ptr) + uintptr(len)) という形で行っていたポインタ演算を 内部で行ってくれるヘルパー関数。

これを利用することで Go でも ポインタ演算 出来るようになる。 ただし、unsafeパッケージを利用する時点でGoの持つ安全性を無くすことに注意が必要。

REFERENCES:

func Alignof added in v0.5.42

func Alignof() error

Alignof は、unsafe.Alignof() のサンプルです。

> Alignof takes an expression x of any type and returns the required alignment of a hypothetical variable v as if v was declared via var v = x. > It is the largest value m such that the address of v is always zero mod m. > It is the same as the value returned by reflect.TypeOf(x).Align(). > As a special case, if a variable s is of struct type and f is a field within that struct, then Alignof(s.f) will return the required alignment of a field of that type within a struct. > This case is the same as the value returned by reflect.TypeOf(s.f).FieldAlign(). > The return value of Alignof is a Go constant if the type of the argument does not have variable size.

> Alignof は任意の型の式 x を受け取り、var v = x のように宣言されたと仮定した場合の、仮想的な変数 v に必要な**アライメント(配置要求)**を返します。 > この戻り値は、v のアドレスが常に m で割って余りが 0 となるような最大の正の整数 m です。これは、reflect.TypeOf(x).Align() が返す値と同じです。 > 特別なケースとして、変数 s が構造体型で、f がその構造体内のフィールドである場合、Alignof(s.f) は構造体内のその型のフィールドに必要なアライメントを返します > このケースは reflect.TypeOf(s.f).FieldAlign() が返す値と同じです。 > 引数の型が可変サイズを持たない場合、Alignof の戻り値はGoの定数になります。

REFERENCES:

func Dump added in v0.5.42

func Dump() error

Dump は、unsafeパッケージを使って構造体のメモリダンプを出力するサンプルです。

func NewRegister

func NewRegister() mapping.Register

NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。

func Offsetof added in v0.5.42

func Offsetof() error

Offsetof は、unsafe.Offsetof() のサンプルです。

> Offsetof returns the offset within the struct of the field represented by x, which must be of the form structValue.field. > In other words, it returns the number of bytes between the start of the struct and the start of the field. > The return value of Offsetof is a Go constant if the type of the argument x does not have variable size.

> Offsetof は、引数 x によって表される構造体内のフィールドのオフセットを返します。引数 x は、必ず structValue.field の形式でなければなりません。 > 言い換えると、これは構造体の先頭からそのフィールドの先頭までのバイト数を返します。 > Offsetof の戻り値は、引数 x の型が可変サイズを持たない場合、Goの定数になります。

REFERENCES:

func PointerCast added in v0.5.42

func PointerCast() error

PointerCast は、unsafeパッケージを用いてポインタを任意の型にキャストするサンプルです。

unsafe.Pointer は、C言語でいう (void *) と同じものとなる。 C言語において、(void *) は何にでも成れるのと同様に unsafe.Pointer はGoでもどの型にもキャスト出来る。 ただし、unsafeパッケージを利用する時点でGoの持つ安全性を無くすことに注意が必要。

REFERENCES:

func Sizeof

func Sizeof() error

Sizeof は、 unsafe.Sizeof() についてのサンプルです.

REFERENCES:

func Slice added in v0.5.42

func Slice() error

Slice は、unsafe.SliceData() と unsafe.Slice() のサンプルです。

unsafe.SliceData() は、特定のスライスを *T に変換する関数。 逆を行ってくれるのが unsafe.Slice() となる。

REFERENCES:

func UnsafeString added in v0.5.19

func UnsafeString() error

UnsafeString は、unsafe.String() のサンプルです.

REFERENCES

func UnsafeStringData added in v0.5.19

func UnsafeStringData() error

UnsafeStringData は、unsafe.StringData() のサンプルです.

REFERENCES

Types

This section is empty.

Jump to

Keyboard shortcuts

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