I'm using a bitwise enum for a Golang project, and I'm storing the bitwise values as a bit(8)
into Postgresql. The type of enum is a uint8
as there are only 8 bits for the enum value.
When I persist information to the database and get back values for the bit(8)
column, it's returning a slice of values. The Scan(...)
method should convert the value
to a uint8
so that the proper Enum
can be created.
Here is my code to explain it more:
type Enum uint8
const (
A enum = 1 << 0
B enum = 1 << 1
C enum = 1 << 2
)
// This method works great. This method is the value the driver uses to
// persist information into the database.
func (u Enum) Value() (driver.Value, error) {
return fmt.Sprintf("%08b", u), nil
}
// This value is responsible for reading info from the database and
// storing it in a Go type.
func (u *Enum) Scan(value interface{}) error {
// This does NOT work as I'm getting back `[00110000 00110000
// 00110000 00110000 00110000 00110000 00110001 00110001]` when I
// fmt.Println(value).
*u = Enum(value.(uint8))
return nil
}
The value
literally looks like 00000011
in the database. So I'm having trouble for converting the the value received from the driver to into the proper enum value.