Why would you prefer to use an empty struct{}?

Technology CommunityCategory: GolangWhy would you prefer to use an empty struct{}?
VietMX Staff asked 3 years ago

You would use an empty struct when you would want to save some memory. Empty structs do not take any memory for its value.

a := struct{}{}
println(unsafe.Sizeof(a))
// Output: 0

This saving is usually insignificant and is dependent on the size of the slice or a map. Although, more important use of an empty struct is to show a reader you do not need a value at all. Its purpose in most cases is mainly informational.