Home > Back-end >  The 2020-10-01: talk about golang empty structure.
The 2020-10-01: talk about golang empty structure.

Time:10-01

# 2020-10-01: talk about golang empty structure, the architects a daily topic # greatly

CodePudding user response:

How to go to the c + + version?

Empty structure and other structures, simply because there is no member, so do not take up memory (sizeof is 0), and two empty structure object values are equal (because no members), and in view of the empty structure application memory, return is a fixed address (address remains the same, such as the type T struct {} func T * (T) printAddr () {FMT. Printf (" % p, T)} when using var t1, t1, t2 T printAddr (") and (t2) printAddr () the result is the same, namely the t1 and t2 is the same address),
According to these characteristics, as far as possible in place of some don't want to use memory use empty structure, such as the channel channel (only need to pass a message to send and receive, don't need the actual data); Only need to call the object's interface methods, for example, does not require objects with data information (such as the type T struct {} func T * (T) printAddr () {FMT. Printf (" % p, T)} var t1 T sizeof (t1) do not take up the data memory, but you can call a t1 printAddr ()), if the objects are in the collection, can greatly save memory,

  • Related