@@ -799,6 +799,46 @@ has been stored in the interface, the interface will not be `nil`.
799
799
For more information, see
800
800
[ The Laws of Reflection] ( /doc/articles/laws_of_reflection.html ) .
801
801
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
+
802
842
### Why are there no untagged unions, as in C? {#unions}
803
843
804
844
Untagged unions would violate Go's memory safety
0 commit comments