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条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误