You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose we add empty as an alias for the empty struct, struct{}
Example:
map[string]struct{}
could be written as:
map[string]empty
Rationale
struct{} has a couple of very attractive qualities:
It allocates no memory
It semantically indicates an empty value, whereas the most common alternative of bool, frequently makes code less readable, by requiring the reader to consider whether the boolean value is meaningful.
These make the use of struct{} ideal for certain use cases, such as channels that only need to signal an event, but no other information about the event, or a map indicating the existence of a thing. i.e. map[string]struct{}{}
However, struct{} has (at least) two drawbacks that prevent it from being used:
It's a bit awkward to type/read, and subjectively "ugly"
It's not obvious (Based on my purely subjective experience reviewing the code of many new Go developers) to Go newcomers that struct{} is even valid, let alone sometimes desirable for the above reasons
I believe that this addition would improve code readability, as well as encourage the use of constructs like make(chan empty) and make(map[string]empty) in place of make(chan bool) and make(map[string]bool) respectively.
References
A quick and lazy search finds over 1200 instances of struct{} in the stdlib (Go 1.20.5)
Naming considerations
empty may not be the best name. In particular, the most common use of the word 'empty' in Go vernacular has been to refer to the empty interface. If this would be seen as conflating with that, perhaps another name would be ideal.
The text was updated successfully, but these errors were encountered:
What
I propose we add
empty
as an alias for the empty struct,struct{}
Example:
could be written as:
Rationale
struct{}
has a couple of very attractive qualities:bool
, frequently makes code less readable, by requiring the reader to consider whether the boolean value is meaningful.These make the use of
struct{}
ideal for certain use cases, such as channels that only need to signal an event, but no other information about the event, or a map indicating the existence of a thing. i.e.map[string]struct{}{}
However,
struct{}
has (at least) two drawbacks that prevent it from being used:struct{}
is even valid, let alone sometimes desirable for the above reasonsI believe that this addition would improve code readability, as well as encourage the use of constructs like
make(chan empty)
andmake(map[string]empty)
in place ofmake(chan bool)
andmake(map[string]bool)
respectively.References
struct{}
in the stdlib (Go 1.20.5)Naming considerations
empty
may not be the best name. In particular, the most common use of the word 'empty' in Go vernacular has been to refer to the empty interface. If this would be seen as conflating with that, perhaps another name would be ideal.The text was updated successfully, but these errors were encountered: