Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bitsets in golang to represent capabilities

Tags:

go

I am new to Golang and would like to model physical devices for measuring quantities such as light intensity, mass, electric current and so forth. So as a starting point I will define a device struct as follows:

const (
    // Light can be measured in the form of luminosity
    Light             = 1<< iota
    Mass             
    ElectricalCurrent 
)

type PhysicalDevice struct{
   Owner       string
   ID          string
   Description string
}

I am a confused now on how to express the device's capabilities (what it can measure) and the units of measurement. For example I would like to express that a physical device can measure electrical currents in amperes. However, I also want to express that a PhysicalDevice can measure more than one quantity. For example it could measure electrical current and temperature.

The PhysicalDevice's capabilities are not known in advance and can contain an arbitrary combination of capabilities.

I was thinking of using something equivalent to a C++ bitset for expressing the physical quantities a device can measure (would this be the right approach in the first place?).

I did not find the Go bitset type and not sure how to express that. I also need to map the measured physical quantity to a corresponding unit.

like image 802
BigONotation Avatar asked Dec 18 '22 02:12

BigONotation


1 Answers

You should understand that trying to replicate another language's features in Go is generally regarded as a bad idea. There is a 'go' way of doing things.

You might want to consider iota and bitmask operations like this example on the Go playground. I have included the code here as well (in all its plagiarized glory):

package main

import "fmt"

func main() {
    TestAddFlag()
    TestHasFlag()
    TestClearFlag()
    TestToggleFlag()

    fmt.Println("all tests passed")
}

type Bitmask uint32

func (f Bitmask) HasFlag(flag Bitmask) bool { return f&flag != 0 }
func (f *Bitmask) AddFlag(flag Bitmask)     { *f |= flag }
func (f *Bitmask) ClearFlag(flag Bitmask)   { *f &= ^flag }
func (f *Bitmask) ToggleFlag(flag Bitmask)  { *f ^= flag }

const (
    TESTFLAG_ONE Bitmask = 1 << iota
    TESTFLAG_TWO
    TESTFLAG_THREE
)

func TestAddFlag() {

    var mainFlag Bitmask = TESTFLAG_TWO

    mainFlag.AddFlag(TESTFLAG_THREE)

    if mainFlag&(1<<TESTFLAG_THREE) != 0 {
        panic("failed")
    }

}

func TestClearFlag() {

    var mainFlag Bitmask = TESTFLAG_ONE | TESTFLAG_THREE

    mainFlag.ClearFlag(TESTFLAG_THREE)

    if mainFlag&(1<<TESTFLAG_ONE) != 0 {
        panic("failed")
    }

}

func TestHasFlag() {

    var mainFlag Bitmask = TESTFLAG_ONE | TESTFLAG_THREE

    if !mainFlag.HasFlag(TESTFLAG_THREE) {
        panic("failed")
    }

}

func TestToggleFlag() {
    flag := TESTFLAG_ONE | TESTFLAG_THREE

    flag.ToggleFlag(TESTFLAG_ONE)
    if flag.HasFlag(TESTFLAG_ONE) {
        panic("failed")
    }
    flag.ToggleFlag(TESTFLAG_ONE)
    if !flag.HasFlag(TESTFLAG_ONE) {
        panic("failed")
    }
}

This approach is commonly used in the standard library.

like image 192
Dan Esparza Avatar answered Jan 11 '23 15:01

Dan Esparza