38

Why would you create a type with empty struct?

type FrontierSigner struct{}

What is it good for?

Nulik
  • 6,748
  • 10
  • 60
  • 129
  • 3
    https://dave.cheney.net/2014/03/25/the-empty-struct –  Nov 29 '17 at 02:39
  • 1
    https://stackoverflow.com/questions/20793568/golang-anonymous-struct-and-empty-struct https://stackoverflow.com/questions/22770114/any-difference-in-using-an-empty-interface-or-an-empty-struct-as-a-maps-value-i – Josh Lee Nov 29 '17 at 02:42
  • @ChronoKitsune, I read it before posting, but I still don't understand. What is a "method receiver" in the first place? – Nulik Nov 29 '17 at 02:45
  • Method receivers are covered in the Go tour: https://tour.golang.org/methods/1 – Adrian Nov 29 '17 at 14:23
  • @Adrian, why would someone use a null method receiver if you can omit it in the first place? You are just doing extra typing work. – Nulik Nov 29 '17 at 16:09
  • I'm not sure I understand the question, but I do know that the goal of programming is not to minimize typing. – Adrian Nov 29 '17 at 16:17
  • @Adrian, in this case, this typing isn't producing any benefits, and I am sure the goal of programming is not to fill your code with pointless annotations. – Nulik Nov 29 '17 at 16:23

3 Answers3

97

Empty struct struct{} is realized in a special way in Go.

  1. It’s a smallest building block in Go. It’s size is literally 0 bytes.

  2. If it has zero size. you may create a slice of 1000’s empty structures and this slice will be very tiny. Because really Go stores only a number of them in the slice but not them itself. The same story with channels.

  3. All pointers to it always point to the same special place in memory.

  4. Very useful in channels when you notify about some event but you don’t need to pass any information about it, only a fact. Best solution is to pass an empty structure because it will only increment a counter in the channel but not assign memory, copy elements and so on. Sometime people use Boolean values for this purpose, but it’s much worse.

  5. Zero size container for methods. You may want have a mock for testing interfaces. Often you don’t need data on it, just methods with predefined input and output.

  6. Go has no Set object. But can be easily realized as a map[keyType]struct{}. This way map keeps only keys and no values.

  7. An empty struct is used as a type to implement an interface. This is seen in receiver methods.

Saurabh
  • 5,176
  • 4
  • 32
  • 46
Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59
9

I usually use it where I would have used a channel of booleans. ie, instead of;

func main() {
    done := make(chan bool, 1)

    go func() {
        // simulate long running task
        time.Sleep(4 * time.Second)
        done <- true
        fmt.Println("long running task is done")
    }()

    <-done
    close(done)

    fmt.Printf("whole program is done.")
}

I use;

package main

import (
    "fmt"
    "time"
)

func main() {
    done := make(chan struct{}, 1)

    go func() {
        // simulate long running task
        time.Sleep(4 * time.Second)
        done <- struct{}{}
        fmt.Println("long running task is done")
    }()

    <-done
    close(done)

    fmt.Printf("whole program is done.")
}
Komu
  • 14,174
  • 2
  • 28
  • 22
  • 1
    It is simpler to close the channel in the spawned go routine. It is also unnecessary to close the channel if you choose to push a value instead. – jdizzle Sep 05 '21 at 20:17
1

Here is another usage of empty struct per link

  • Empty struct{} to group methods together
  • Methods don't capture the receiver
type Codec interface {
    Encode(w io.Writer, v interface{}) error
    Decode(r io.Reader, v interface{}) error
}    

type jsonCodec struct{}
func (jsonCodec) Encode(w io.Writer, v interface{}) error {
    return json.NewEncoder(w).Encode(v)
}
func (jsonCodec) Decode(r io.Reader, v interface{}) error {
    return json.NewDecoder(r).Decode(v)
}    

var JSON Codec = jsonCodec{}

func main() {
    sobj := struct {
        S1 string `json:"s1"`
        K3 string `json:"k3"`
    }{}
    ss := `{"s1": "v1", "k3": "vv3"}`
    err := JSON.Decode(strings.NewReader(ss), &sobj)
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println(sobj)
}

It is one simple API, and JSON variable doesn't expose the jsonCodec type

zangw
  • 43,869
  • 19
  • 177
  • 214