-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (59 loc) · 1.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"fmt"
"unsafe"
)
/* Size and types:
* string - 16 bytes
* int32 - 4 bytes
* int64 - 8 bytes
* int - depends on system architecture (32 bits - 4 bytes or 64 bits - 8 bytes)
* uint - equal rules for int type
* float32 - 4 bytes
* float64 - 8 bytes
* bool - 1 byte
*/
type StructA struct {
i32 int32 // 4 bytes
s string // 16 bytes
b bool // 1 bytes
}
type StructAOptimized struct {
i32 int32 // 4 bytes
b bool // 1 bytes
s string // 16 bytes
}
type StructB struct {
i32 int32 // 4 bytes
s string // 16 bytes
f32 float32 // 4 bytes
}
type StructBOptimized struct {
i32 int32 // 4 bytes
f32 float32 // 4 bytes
s string // 16 bytes
}
type StructC struct {
b bool // 1 byte
i64 int64 // 8 bytes
i32 int32 // 4 bytes
}
type StructCOptimized struct {
b bool // 1 byte
i32 int32 // 4 bytes
i64 int64 // 8 bytes
}
func main() {
a := StructA{}
aOptimized := StructAOptimized{}
b := StructB{}
bOptimized := StructBOptimized{}
c := StructC{}
cOptimized := StructCOptimized{}
fmt.Println("Size of Struct A: ", unsafe.Sizeof(a)) // 32 bytes
fmt.Println("Size of Struct A Optimized: ", unsafe.Sizeof(aOptimized)) // 24 bytes
fmt.Println("Size of Struct B: ", unsafe.Sizeof(b)) // 32 bytes
fmt.Println("Size of Struct B Optimized: ", unsafe.Sizeof(bOptimized)) // 24 bytes
fmt.Println("Size of Struct C: ", unsafe.Sizeof(c)) // 24 bytes
fmt.Println("Size of Struct C Optimized: ", unsafe.Sizeof(cOptimized)) // 16 bytes
}