douquanjie9326 2016-01-15 00:43
浏览 21
已采纳

是否有可能说服Golang编译器接受“ Foo int类型”作为“ int”?

I'm using the pebbe/zmq4 ZeroMQ bindings for Go, and I'm trying to develop higher level interfaces for my code that ZeroMQ implements in order to support mocking in my tests.

As an example of my question, the zmq4.Socket struct's RecvMessage function expects a zmq4.Flag as an argument. zmq4.Flag is simply an int, as defined by type Flag int in the Go bindings.

I'm trying to develop my interfaces without any dependencies on the ZeroMQ bindings, so I have an interface defined as:

type Socket interface {
    RecvMessage(int) ([]string, error)
}

When I try to use a ZeroMQ socket for this interface, I get an error stating ... have RecvMessage(zmq4.Flag) ([]string, error) want RecvMessage(int) ([]string, error).

Is there any way to handle this, or do I just need to bite the bullet and depend on the ZeroMQ bindings in my interfaces?

  • 写回答

1条回答 默认 最新

  • duanlei4759 2016-01-15 01:15
    关注

    It is important to realize that type Foo int is a separate type not an alias. See How to cast to a type alias in Go?

    The only thing you can do to call RecvMessage with zmq4.Flag is to convert it to int.

    var f zmq4.Flag = 1
    RecvMessage(int(f))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题