Home > Backend Development > Golang > Anonymous Structs vs. Empty Structs in Go: Why Use `struct{}` for Goroutine Synchronization?

Anonymous Structs vs. Empty Structs in Go: Why Use `struct{}` for Goroutine Synchronization?

DDD
Release: 2025-01-04 05:10:39
Original
831 people have browsed it

Anonymous Structs vs. Empty Structs in Go: Why Use `struct{}` for Goroutine Synchronization?

Anonymous Structs vs. Empty Structs in Go

Consider the following code snippet, which employs a channel of type struct{} to facilitate communication between goroutines:

package main

import "fmt"

var battle = make(chan string)

func warrior(name string, done chan struct{}) {
    select {
    case opponent := <-battle:
        fmt.Printf("%s beat %s\n", name, opponent)
    case battle <- name:
        // I lost :-(
    }
    done <- struct{}{}
}

func main() {
    done := make(chan struct{})
    langs := []string{"Go", "C", "C++", "Java", "Perl", "Python"}
    for _, l := range langs { go warrior(l, done) }
    for _ = range langs { <-done }
}
Copy after login

[Question 1]

The line done <- struct{}{}, which sends an empty struct to the done channel, has sparked confusion. While an ordinary struct can be declared with struct { /* fields */ }, an empty struct is denoted by struct{}{}, which prompts a few questions:

  • Why is this syntax necessary?
  • Is this an anonymous struct or an empty struct?

An empty struct is a struct that doesn't have any fields. It's also known as an anonymous struct. The purpose of an empty struct is to provide a way to create a type that has no fields, which can be useful for various reasons, such as:

  • As a placeholder for a type that may be added in the future
  • As a way to create a unique type that can be used for synchronization
  • As a way to create a type that can be used as a key in a map

In our code, we use an anonymous struct to signal that a goroutine has finished its work. Using an empty struct for this purpose is advantageous because it has a size of 0, minimizing memory footprint.

[Question 2]

The line for _ = range langs { <-done } is responsible for receiving and discarding values from the done channel. This line is necessary for the following reasons:

  • Without this line, the program would not produce any output because the main goroutine would exit before the other goroutines had finished their work.
  • The loop receives values from the done channel, but discards them using the _ wildcard variable. This is because we are not interested in the actual values sent by the goroutines. We simply want to wait until all goroutines have signaled their completion.

By using a channel of type struct{} and an empty struct as a signaling mechanism, we can achieve efficient communication between goroutines with minimal overhead.

The above is the detailed content of Anonymous Structs vs. Empty Structs in Go: Why Use `struct{}` for Goroutine Synchronization?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template