drvlf9739 2018-01-01 14:34
浏览 58
已采纳

使用golang中的位集表示功能

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.

  • 写回答

2条回答 默认 最新

  • duanji5746 2018-01-01 15:51
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?