douhuiyan2772 2014-01-24 14:36
浏览 91
已采纳

指向Go中的接口的指针

I'm currently reading the source code of the https://github.com/codegangsta/inject go package to understand how does this package works.

I have some questions concerning the file https://github.com/codegangsta/inject/blob/master/inject.go file thats use some element of the Go language I don't understand and don't find precise explanations in the documentation.

// InterfaceOf dereferences a pointer to an Interface type.
// It panics if value is not an pointer to an interface.

func InterfaceOf(value interface{}) reflect.Type {
        t := reflect.TypeOf(value)

        for t.Kind() == reflect.Ptr {
                t = t.Elem()
        }

        if t.Kind() != reflect.Interface {
                panic("Called inject.InterfaceOf with a value that is not a pointer to an interface. (*MyInterface)(nil)")
        }

        return t
}

My first question is concerning the for loop. Why does it uses a for loop with a test expression ?

The second relates to the message in the panic function. "A pointer to an interface" is mentioned with the (*MyInterface)(nil). I only encounter a similar construction in the go documentation concerning 'compile time checking structure' when you check that a type implements a structure :

var _ SomeType = (*SomeInterface)(nil)

I did not find any informations about a statement with (*Interface)(nil) and pointer to interface.

How should we interpret this statement ? What is the relation with a pointer to interface and where could I find informations about pointer to interface ?

展开全部

  • 写回答

3条回答 默认 最新

  • douhei8633 2014-01-24 19:34
    关注

    To summarize both answers:

    The for loop

    for t.Kind() == reflect.Ptr {
        t = t.Elem()
    }
    

    t.Elem() is the reflection equivalent to *t, so what this loop does it dereferencing t as long as it holds another pointer value. At the end of the loop, t will hold the value that the last pointer pointed to, not a pointer anymore.

    The message

    Called [...] with a value that is not a pointer to an interface. (*MyInterface)(nil)

    The expression (*MyInterface)(nil) is just an (poorly phrased) example of what is expected as parameter.

    The syntax is that of a conversion. A conversion will attempt to convert a value (in this case nil) to a given type (*MyInterface) in this case. So,

    (*MyInterface)(nil) 
    

    will give you a zero value of a *MyInterface whose interface type would be MyInterface (play):

    x := (*MyInterface)(nil)
    InterfaceOf(x) // MyInterface
    

    Of course, this value does not point somewhere meaningful.

    Compile time checking of interface implementation

    To avoid confusion, the construct you showed

    var _ SomeType = (*SomeInterface)(nil)

    is probably not what you wanted. I guess you wanted this:

    var _ SomeInterface = (*SomeType)(nil)
    

    This construct enables compile time checking of interface implementation for certain types. So in case you're writing a library of some sort and you want to satisfy an interface without using it, you can use this to make sure that your struct implements the interface.

    Why this works

    First of all, var _ someType is a variable that is going to be checked by the compiler but will not be in the compiled program and is not accessible due to the Blank Identifier _:

    The blank identifier may be used like any other identifier in a declaration, but it does not introduce a binding and thus is not declared.

    This enables you do declare an arbitrary number of these constructs without interfering with the rest of the program.

    You can declare a zero value of a pointer of any type by writing:

    (*T)(nil)
    

    Check this example on play.

    Next, assignability says that x is assignable to T if T is an interface and x implements T.

    So to summarize:

    T _ = (*x)(nil)
    

    enforces that x implements T as everything else would be an error.

    展开全部

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

报告相同问题?

悬赏问题

  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥30 怎么把PCK、OKS指标添加到yolov11中
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 求GCMS辅导数据分析
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部