dongyang4615 2014-05-18 16:14
浏览 6

新结构的返回类型是什么?

For example, I have this struct:

type MapReduce struct {
    nMap            int    // Number of Map jobs
    nReduce         int    // Number of Reduce jobs
    file            string // Name of input file
    MasterAddress   string
    registerChannel chan string
    DoneChannel     chan bool
    alive           bool
    l               net.Listener
    stats           *list.List

    // Map of registered workers that you need to keep up to date
    Workers map[string]*WorkerInfo

    // add any additional state here
}

Then I new it like this:

mr := new(MapReduce)

Then I use it like this:

rpcs := rpc.NewServer()
rpcs.Register(mr)

My question is ,, rpc.Register takes interface as argument. http://golang.org/pkg/net/rpc/#Server but mr here is not interface, why is it right?

Thanks

  • 写回答

1条回答 默认 最新

  • dongyao4003 2014-05-18 16:16
    关注

    It does take an empty interface type interface{}, which is satisfied by any type.

    So you can pass a *MapReduce to the Register(interface{}) method.

    From spec interface type:

    A type implements any interface comprising any subset of its methods and may therefore implement several distinct interfaces.
    For instance, all types implement the empty interface:

    interface{}
    

    Bit remember, once it is passed, its static type become interface{}.
    Law of reflection mentions:

    Some people say that Go's interfaces are dynamically typed, but that is misleading.

    They are statically typed: a variable of interface type always has the same static type, and even though at run time the value stored in the interface variable may change type, that value will always satisfy the interface.

    See more at "what is the meaning of interface{} in golang?".

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划