ALL


  What is the use of empty struct in GoLang

In Go, an empty struct struct{} is a struct with no fields that may appear to be of little use, but in reality, it can be useful in certain situations and become a simple and efficient solution in code.As a semaphore or lockBecause the empty struct has no fields, it can be conveniently used to implement some concurrency control functions, such as mutex locks, read-write locks. We can use chan struct{} to implement an unbuffered channel for controlling concurrent access.package mainimport ( "fmt" "sync")func main() { var wg sync.WaitGroup var mu sync.Mutex c := make(chan struct{}...

9,484 2       GOLANG EMPTY STRUCT