Skip to content

Commit a5f55a2

Browse files
ianlancetaylorgopherbot
authored andcommitted
_content/doc/faq: add entry about zero-size types
Provide a reference to help clear up periodic confusion: https://groups.google.com/g/golang-nuts/c/MXcOqW-Mf-c/m/2-24iQXSAwAJ https://groups.google.com/g/golang-nuts/c/JBVqWYFdtC4/m/EqZxT9EYAQAJ For golang/go#2620 For golang/go#11925 For golang/go#23440 For golang/go#47950 For golang/go#52772 For golang/go#58483 For golang/go#65878 Change-Id: I0986b35d02f2fe88d5a08af1f7d1f50510c39d7c Reviewed-on: https://go-review.googlesource.com/c/website/+/594515 Reviewed-by: Rob Pike <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]>
1 parent a36b46a commit a5f55a2

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

_content/doc/faq.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,46 @@ has been stored in the interface, the interface will not be `nil`.
799799
For more information, see
800800
[The Laws of Reflection](/doc/articles/laws_of_reflection.html).
801801

802+
### Why do zero-size types behave oddly? {#zero_size_types}
803+
804+
Go supports zero-size types, such as a struct with no fields
805+
(`struct{}`) or an array with no elements (`[0]byte`).
806+
There is nothing you can store in a zero-size type, but these types
807+
are sometimes useful when no value is needed, as in
808+
`map[int]struct{}` or a type that has methods but no value.
809+
810+
Different variables with a zero-size type may be placed at the same
811+
location in memory.
812+
This is safe as no value can be stored in those variables.
813+
814+
Moreover, the language does not make any guarantees as to whether
815+
pointers to two different zero-size variables will compare equal or
816+
not.
817+
Such comparisons may even return `true` at one point in the program
818+
and then return `false` at a different point, depending on exactly how
819+
the program is compiled and executed.
820+
821+
A separate issue with zero-size types is that a pointer to a zero-size
822+
struct field must not overlap with a pointer to a different object in
823+
memory.
824+
That could cause confusion in the garbage collector.
825+
This means that if the last field in a struct is zero-size, the struct
826+
will be padded to ensure that a pointer to the last field does not
827+
overlap with memory that immediately follows the struct.
828+
Thus, this program:
829+
830+
```
831+
func main() {
832+
type S struct {
833+
f1 byte
834+
f2 struct{}
835+
}
836+
fmt.Println(unsafe.Sizeof(S{}))
837+
}
838+
```
839+
840+
will print `2`, not `1`, in most Go implementations.
841+
802842
### Why are there no untagged unions, as in C? {#unions}
803843

804844
Untagged unions would violate Go's memory safety

0 commit comments

Comments
 (0)