Skip to content

Commit 00c975c

Browse files
committed
Update struct_memory_pading.go
1 parent 0ff6620 commit 00c975c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

internal/examples/basic/structs/struct_memory_padding.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,22 @@ import (
1010
// MemoryPadding は、構造体メンバーの定義順によってGoランタイムがメモリ上にパディングを挿入することを確認するサンプルです.
1111
func 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

internal/examples/basic/structs/types/memoryPadding.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package 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 は、メンバー定義順によってメモリのパディングが発生する構造体です.
416
type MemoryPadding struct {
517
Flag1 bool

0 commit comments

Comments
 (0)