Skip to content

Commit 505fa7b

Browse files
cmd/cgo: document C.sizeof_T and zero-sized field restriction
Update #9401. Update #11925. Update #13919. Change-Id: I52c679353693e8165b2972d4d3974ee8bb1207ef Reviewed-on: https://go-review.googlesource.com/18542 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent eca3618 commit 505fa7b

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/cmd/cgo/doc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,19 @@ The C types __int128_t and __uint128_t are represented by [16]byte.
125125
To access a struct, union, or enum type directly, prefix it with
126126
struct_, union_, or enum_, as in C.struct_stat.
127127
128+
The size of any C type T is available as C.sizeof_T, as in
129+
C.sizeof_struct_stat.
130+
128131
As Go doesn't have support for C's union type in the general case,
129132
C's union types are represented as a Go byte array with the same length.
130133
131134
Go structs cannot embed fields with C types.
132135
136+
Go code can not refer to zero-sized fields that occur at the end of
137+
non-empty C structs. To get the address of such a field (which is the
138+
only operation you can do with a zero-sized field) you must take the
139+
address of the struct and add the size of the struct.
140+
133141
Cgo translates C types into equivalent unexported Go types.
134142
Because the translations are unexported, a Go package should not
135143
expose C types in its exported API: a C type used in one Go package

0 commit comments

Comments
 (0)