Friday, April 26, 2024
HomeGolangWhy use struct{}{}

Why use struct{}{}


This text makes use of these constructs:

executed := make(chan struct{}, 2)
executed <- struct{}{}

Is struct{} an idiom for a sort whose content material doesn’t matter, however its presence does?
Is it the smallest kind? Does it take much less area than bool, or byte? Is its measurement zero?
A ran a check, and I feel the reply is sure to all:

package deal most important

import (
    "fmt"
)

func most important() {
    for i := 0; i < 100; i++ {
        a := make([]struct{}, 4000000000)
        fmt.Println(i, len(a))
    }
}

This program runs in a short time.
If I exploit byte as an alternative of struct{}, this system will get caught searching for reminiscence.

An arrray of 0 measurement (reminiscent of [0]bool additionally has this property of zero measurement.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments