dthjnc306679 2018-03-09 10:16
浏览 353

如何在golang中动态更改类型定义?

I have come across a scenario where I have to use a variable to count up to, say, 255 or 65535 (depending on a flag) and overflow to 0. So when I set the flag to say, true, the type definition should be uint8 and when false, the type definition should be uint16. Obviously I can set the master variable as uint16 and perform mathematical manipulations on them, but is there a native solution for this?

  • 写回答

1条回答 默认 最新

  • dongxin5429 2018-03-09 10:33
    关注

    Go is a statically typed language, which means types of variables must be known at compile time, and you can't change their type at runtime.

    Counter with a struct

    Instead create your own counter type with a method responsible to increment its value, which can check if the counter's max value is exceeded, in which case it can reset it.

    type Counter struct {
        value, Max int
    }
    
    func (c *Counter) Inc() {
        c.value++
        if c.value > c.Max {
            c.value = 0
        }
    }
    
    func (c *Counter) Value() int { return c.value }
    

    Example using it:

    c := &Counter{Max: 255}
    
    for i := 0; i < 257; i++ {
        c.Inc()
        if i > 252 {
            fmt.Println(c.Value())
        }
    }
    

    Output (try it on the Go Playground):

    254
    255
    0
    1
    

    So in your case use it like this:

    c := &Counter{}
    if flagUse8bit {
        c.Max = 0xff
    } else {
        c.Max = 0xffff
    }
    

    Counter with interfaces

    Another nice solution would be to use interfaces, as a variable of interface type can hold a value of any type that implements that interface.

    Create a Counter interface type:

    type Counter interface {
        Inc()
        Value() int
    }
    

    And have different implementations for different types, simply using different underlying types, exploiting the fact that overflow will happen naturally. And you may choose the runtime type to store in the Counter interface variable based on the CLI flag value.

    type counter8 uint8
    
    func (c *counter8) Inc()      { *c++ }
    func (c counter8) Value() int { return int(c) }
    
    type counter16 uint16
    
    func (c *counter16) Inc()      { *c++ }
    func (c counter16) Value() int { return int(c) }
    

    And using it:

    var c Counter
    
    c = new(counter8)
    for i := 0; i < 257; i++ {
        c.Inc()
        if i > 252 {
            fmt.Println(c.Value())
        }
    }
    
    c = new(counter16)
    for i := 0; i < 65537; i++ {
        c.Inc()
        if i > 65532 {
            fmt.Println(c.Value())
        }
    }
    

    Output (try it on the Go Playground):

    254
    255
    0
    1
    65534
    65535
    0
    1
    

    So in your case use it like this:

    var c Counter
    if flagUse8bit {
        c = new(counter8)
    } else {
        c = new(counter16)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错