Preface
Through this articleA brief discussion on the basic use of Go structure》, we have a preliminary understandingEmpty structure,use()
Method, it clearly knows that the empty structure does not occupy storage space (that is, the "width" is 0, and the width describes the number of bytes of the storage space occupied by an instance of a type)
s := struct{}{} ((s)) //0
In project code, we often see empty structuresstruct{}{}
Therefore, there must be some reasons behind it. So what is its function and what scenarios are suitable for?
effect
Because the empty structure does not occupy memory space,It is widely used as a placeholder in various scenarios. First, save resources, and second, the empty structure itself has strong semantics, that is, no value is needed here, it is only used as a placeholder.
Use scenarios
There are 3 main usage scenarios:
- Implement collection type
- Implement empty channels
- Implement method recipient
Implement collection type
Go language itself does not have a collection type (Set), and it is usually used to replace it with map.。
But there is a problem, that is, the collection type only needs to use the key (key) and not the value (value)
If the value is represented by bool, it will actually occupy 1 byte of space. In order to save space, the space-time structure can show its skills
type Set map[int]struct{} func main() { s := make(Set) (1) (2) (3) (2) ((1)) (s) //Output: //true //map[1:{} 3:{}] } func (s Set) add(num int) { s[num] = struct{}{} } func (s Set) remove(num int) { delete(s, num) } func (s Set) exist(num int) bool { _, ok := s[num] return ok }
As a placeholder, the empty structure will not add unnecessary memory overhead, which will solve the problem very conveniently.
Implement empty channels
In the use of Go channel, you often encounterNotification channel, it does not need to send any data, but is only used to coordinate the operation of Goroutine, to flow various states or control concurrency.
This kind of situation is particularly suitable for using empty structures, only a placeholder and no memory space is wasted
func main() { ch := make(chan struct{}) go worker(ch) // Send a message to a worker. ch <- struct{}{} // Receive a message from the worker. <-ch println("AAA") //Output: //BBB //AAA } func worker(ch chan struct{}) { // Receive a message from the main program. <-ch println("BBB") // Send a message to the main program. close(ch) }
Since the channel uses an empty structure, it does not cause additional memory overhead
Implement method recipient
Using variables of the structure type as method recipients, sometimes the structure may not contain any field attributes. In this case, int or string can be used instead, but they all occupy memory space, so it is more appropriate to continue using empty structures.
andIt is also conducive to the addition of public fields, etc. for this type in the future, and is easy to expand and maintain.
type T struct{} func methodUse() { t := T{} () t.Print2() //Output: //Hahaha Print //Hahaha Print2} func (t T) Print() { ("Hahaha Print") } func (t T) Print2() { ("Hahaha Print2") }
Summarize
A detailed explanation was given on the role and use scenarios of empty structures. In the subsequent actual project development process,Only placeholding does not mean it, so we can all use empty structures, can greatly save unnecessary memory overhead.
This is the end of this article about what the function of Go empty structure struct{} is. For more related Go empty structure struct{}, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!