Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What uses a type with empty struct has in Go?

Tags:

go

Why would you create a type with empty struct?

type FrontierSigner struct{}

What is it good for?

like image 523
Nulik Avatar asked Nov 29 '17 02:11

Nulik


People also ask

What is use of empty struct in Golang?

What's an empty struct? An empty struct is a struct type without fields struct{} . The cool thing about an empty structure is that it occupies zero bytes of storage. You can find an accurate description about the actual mechanism inside the golang compiler in this post by Dave Chaney.

How do you check if struct is empty or not in Golang?

1) To check if the structure is empty:fmt. Println( "It is an empty structure." ) fmt. Println( "It is not an empty structure." )

What is type struct go?

A structure or struct in Golang is a user-defined type that allows to group/combine items of possibly different types into a single type. Any real-world entity which has some set of properties/fields can be represented as a struct. This concept is generally compared with the classes in object-oriented programming.

How do you initialize a struct in Go?

2 ways to create and initialize a new structThe new keyword can be used to create a new struct. It returns a pointer to the newly created struct. You can also create and initialize a struct with a struct literal. An element list that contains keys does not need to have an element for each struct field.


2 Answers

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. Bit can be easily realized as a map[keyType]struct{}. This way map keeps only keys and no values.

like image 117
Eugene Lisitsky Avatar answered Oct 17 '22 18:10

Eugene Lisitsky


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.")
}
like image 43
Komu Avatar answered Oct 17 '22 16:10

Komu