Question:

Why would you prefer to use an empty struct{}? Provide some examples of the good use of the empty struct{}.

Answer:

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. Here are a few examples where it can be useful:

  • When implementing a data set:
set := make(map[string]struct{})
for _, value := range []string{"apple", "orange", "apple"} {
   set[value] = struct{}{}
}
fmt.Println(set)
// Output: map[orange:{} apple:{}]
  • With the seen hash, like when traversing a graph:
seen := make(map[string]struct{})
for _, ok := seen[v]; ok {
    // First time visiting a vertex.
    seen[v] = struct{}{}
}
  • When building an object, and only being interested in a grouping of methods and no intermediary data, or when you do not plan to retain the object state. In the example below it does not make a difference whether the method is called on the same (case #1) or on two different objects (case #2):
type Lamp struct{}

func (l Lamp) On() {
        println("On")

}
func (l Lamp) Off() {
        println("Off")
}

func main() {
       	// Case #1.
       	var lamp Lamp
       	lamp.On()
       	lamp.Off()
       	// Output:
       	// on
       	// off
	
       	// Case #2.
       	Lamp{}.On()
       	Lamp{}.Off()
       	// Output: 
       	// on
       	// off
}
  • When you need a channel to signal an event, but do not really need to send any data. This event is also not the last one in the sequence, in which case you would use the close(ch) built-in function.
func worker(ch chan struct{}) {
	// Receive a message from the main program.
	<-ch
	println("roger")
	
	// Send a message to the main program.
	close(ch)
}

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(“roger")
	// Output:
	// roger
	// roger
}

Keywords:

© 2017 QuizBucket.org