1 Does an empty structure take up space?
In Go, we can use unsafe.Sizeof
to calculate the number of bytes an instance of a data type needs to occupy.
Running the above example will output:
That is, an instance of the empty struct struct{}
does not occupy any memory space.
2 The role of the empty structure
Because empty structs do not occupy memory space, they are widely used as placeholders in various scenarios. One is to save resources, and the other is that the empty struct itself has a strong semantic meaning that no value is needed here, only as a placeholder.
2.1 Implementing a Set
The Go language standard library does not provide an implementation of Set, and maps are usually used instead. In fact, for a set, only the keys of the map are needed, not the values. Even if the value is set to bool, it will take up an extra 1 byte, so assuming there are a million items in the map, that’s 1MB of wasted space.
Therefore, when using map as a set, you can define the value type as an empty structure and use it only as a placeholder.
|
|
2.2 Channels that do not send data (channel)
Sometimes a channel is used without sending any data, only to inform a subcoordinator goroutine
to perform a task, or only to control a goroutine
. In this case, it is appropriate to use an empty structure as a placeholder.
2.3 Structs containing only methods
In some scenarios, the structure contains only methods and not any fields. For example Door in the example above, in this case Door can in fact be replaced by any data structure. For example
Either int or bool will waste extra memory, so it is most appropriate to declare the structure as empty in this case.
Reference https://geektutu.com/post/hpg-empty-struct.html