Empty struct

suggest change

A struct is a sequence of named elements, called fields, each of which has a name and a type. Empty struct has no fields, like this anonymous empty struct:

var s struct{}

Or like this named empty struct type:

type T struct{}

The interesting thing about the empty struct is that, its size is zero:

fmt.Println(unsafe.Sizeof(s))

This prints 0, so the empty struct itself takes no memory. so it is good option for quit channel, like:

package main

import (
    "fmt"
    "time"
)

func main() {
    done := make(chan struct{})
    go func() {
        time.Sleep(1 * time.Second)
        close(done)
    }()

    fmt.Println("Wait...")
    <-done
    fmt.Println("done.")
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents