liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// run
 
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
 
package main
 
import "unsafe"
 
type T struct {
   X int
}
 
var t T
 
func isUintptr(uintptr) {}
 
type T2 struct {
   A int32
   U2
}
 
type U2 struct {
   B int32
   C int32
}
 
var t2 T2
var p2 *T2
 
func main() {
   // Test unsafe.Sizeof, unsafe.Alignof, and unsafe.Offsetof all return uintptr.
   isUintptr(unsafe.Sizeof(t))
   isUintptr(unsafe.Alignof(t))
   isUintptr(unsafe.Offsetof(t.X))
 
   // Test correctness of Offsetof with respect to embedded fields (issue 4909).
   if unsafe.Offsetof(t2.C) != 8 {
       println(unsafe.Offsetof(t2.C), "!= 8")
       panic("unsafe.Offsetof(t2.C) != 8")
   }
   if unsafe.Offsetof(p2.C) != 8 {
       println(unsafe.Offsetof(p2.C), "!= 8")
       panic("unsafe.Offsetof(p2.C) != 8")
   }
   if unsafe.Offsetof(t2.U2.C) != 4 {
       println(unsafe.Offsetof(t2.U2.C), "!= 4")
       panic("unsafe.Offsetof(t2.U2.C) != 4")
   }
   if unsafe.Offsetof(p2.U2.C) != 4 {
       println(unsafe.Offsetof(p2.U2.C), "!= 4")
       panic("unsafe.Offsetof(p2.U2.C) != 4")
   }
   testDeep()
   testNotEmbedded()
}
 
type (
   S1 struct {
       A int64
       S2
   }
   S2 struct {
       B int64
       S3
   }
   S3 struct {
       C int64
       S4
   }
   S4 struct {
       D int64
       S5
   }
   S5 struct {
       E int64
       S6
   }
   S6 struct {
       F int64
       S7
   }
   S7 struct {
       G int64
       S8
   }
   S8 struct {
       H int64
       *S1
   }
)
 
func testDeep() {
   var s1 S1
   switch {
   case unsafe.Offsetof(s1.A) != 0:
       panic("unsafe.Offsetof(s1.A) != 0")
   case unsafe.Offsetof(s1.B) != 8:
       panic("unsafe.Offsetof(s1.B) != 8")
   case unsafe.Offsetof(s1.C) != 16:
       panic("unsafe.Offsetof(s1.C) != 16")
   case unsafe.Offsetof(s1.D) != 24:
       panic("unsafe.Offsetof(s1.D) != 24")
   case unsafe.Offsetof(s1.E) != 32:
       panic("unsafe.Offsetof(s1.E) != 32")
   case unsafe.Offsetof(s1.F) != 40:
       panic("unsafe.Offsetof(s1.F) != 40")
   case unsafe.Offsetof(s1.G) != 48:
       panic("unsafe.Offsetof(s1.G) != 48")
   case unsafe.Offsetof(s1.H) != 56:
       panic("unsafe.Offsetof(s1.H) != 56")
   case unsafe.Offsetof(s1.S1) != 64:
       panic("unsafe.Offsetof(s1.S1) != 64")
   case unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 8:
       panic("unsafe.Offsetof(s1.S1.S2.S3.S4.S5.S6.S7.S8.S1.S2) != 8")
   }
}
 
func testNotEmbedded() {
   type T2 struct {
       B int32
       C int32
   }
   type T1 struct {
       A int32
       T2
   }
   type T struct {
       Dummy int32
       F     T1
       P     *T1
   }
 
   var t T
   var p *T
   switch {
   case unsafe.Offsetof(t.F.B) != 4:
       panic("unsafe.Offsetof(t.F.B) != 4")
   case unsafe.Offsetof(t.F.C) != 8:
       panic("unsafe.Offsetof(t.F.C) != 8")
 
   case unsafe.Offsetof(t.P.B) != 4:
       panic("unsafe.Offsetof(t.P.B) != 4")
   case unsafe.Offsetof(t.P.C) != 8:
       panic("unsafe.Offsetof(t.P.C) != 8")
 
   case unsafe.Offsetof(p.F.B) != 4:
       panic("unsafe.Offsetof(p.F.B) != 4")
   case unsafe.Offsetof(p.F.C) != 8:
       panic("unsafe.Offsetof(p.F.C) != 8")
 
   case unsafe.Offsetof(p.P.B) != 4:
       panic("unsafe.Offsetof(p.P.B) != 4")
   case unsafe.Offsetof(p.P.C) != 8:
       panic("unsafe.Offsetof(p.P.C) != 8")
   }
}