File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
internal/examples/basic/structs Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -10,13 +10,22 @@ import (
1010// MemoryPadding は、構造体メンバーの定義順によってGoランタイムがメモリ上にパディングを挿入することを確認するサンプルです.
1111func MemoryPadding () error {
1212 var (
13- notGood = types.MemoryPadding {} // メモリのパディングが発生する構造体
14- good = types.NoMemoryPadding {} // メモリのパディングが発生しない構造体
13+ st4bytes = types.Struct4Bytes {} // メモリ上のサイズが 4bytes になる構造体
14+ st8bytes = types.Struct8Bytes {} // メモリ上のサイズが 8bytes になる構造体
15+ notGood = types.MemoryPadding {} // メモリのパディングが発生する構造体
16+ good = types.NoMemoryPadding {} // メモリのパディングが発生しない構造体
1517 )
1618
19+ output .Stdoutf ("[st4bytes]" , "%d byte(s)\n " , unsafe .Sizeof (st4bytes ))
20+ output .StdoutHr ()
21+
22+ output .Stdoutf ("[st8bytes]" , "%d byte(s)\n " , unsafe .Sizeof (st8bytes ))
23+ output .StdoutHr ()
24+
1725 output .Stdoutf ("[Padding 発生]" , "%d byte(s)\n " , unsafe .Sizeof (notGood ))
1826 output .Stdoutl ("" , notGood .Layout ())
1927 output .StdoutHr ()
28+
2029 output .Stdoutf ("[Padding なし]" , "%d byte(s)\n " , unsafe .Sizeof (good ))
2130 output .Stdoutl ("" , good .Layout ())
2231
Original file line number Diff line number Diff line change 11package types
22
3+ // Struct4Bytes は、メンバーのサイズで見ると 3bytes なのに、メモリ上のサイズが 4bytes になる構造体です.
4+ type Struct4Bytes struct {
5+ Flag bool
6+ Value int16
7+ }
8+
9+ // Struct8Bytes は、メンバーのサイズで見ると 5bytes なのに、メモリ上のサイズが 8bytes になる構造体です.
10+ type Struct8Bytes struct {
11+ Flag bool
12+ Value int32
13+ }
14+
315// MemoryPadding は、メンバー定義順によってメモリのパディングが発生する構造体です.
416type MemoryPadding struct {
517 Flag1 bool
You can’t perform that action at this time.
0 commit comments