Custom Bit Lengths in Go?

In Go, is it possible to define a custom type with several bits different from those offered byte uint uint16or by any other built-in type?

I plan to use "enough bits" to represent variables and I need a 6-bit and 4-bit type. Perhaps a complex bool type?

type fourbit struct{
    ones   bool
    twos   bool
    fours  bool
    eights bool
}

Although this kind of thing is pretty dirty, it would be nice to have a more general solution for n-bit types.

+4
source share
1 answer

No. The minimum size of the Go type in current implementations, including the type bool, is one byte,

Literature:

Go programming language specification

+3
source

Source: https://habr.com/ru/post/1613733/


All Articles