golang接口符合性编译类型检查 [英] golang interface compliance compile type check

查看:159
本文介绍了golang接口符合性编译类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从camlistore看到以下声明( http: //code.google.com/p/camlistore/source/browse/pkg/cacher/cacher.go )。

  var(
_ blobref.StreamingFetcher =(* CachingFetcher)(nil)
_ blobref.SeekFetcher =(* CachingFetcher)(nil)
_ blobref.StreamingFetcher =(* DiskCache)(nil)
blobref.SeekFetcher =(* DiskCache)(nil)

我知道没有创建变量,并且这些语句确保编译器检查CachingFether是否实现StreamingFetcher和SeekFetcher的公共函数。 RHS部分使用带有nil参数的指针构造函数语法。这种语法在Go语言中意味着什么?

解决方案

(* T)(nil)转换。在这种情况下,它代表一个输入零,即。例如

  var p * T 

在分配任何东西之前已经有了。



转换的标准语法是 T (expr),但是 * 的优先级会在
<$ p中错误地绑定它$ p> * T(expr)

此语法表示取消引用返回值函数 T 带有一个参数 expr 。这就是为什么转换有一个替代语法:

 (T)(expr)

其中 T 当然可以是 * U 。因此

 (* U)(expr)

是您在Camlistore存储库中看到的一般形式。


I see the following statements from camlistore(http://code.google.com/p/camlistore/source/browse/pkg/cacher/cacher.go).

var (
        _ blobref.StreamingFetcher = (*CachingFetcher)(nil)
        _ blobref.SeekFetcher      = (*CachingFetcher)(nil)
        _ blobref.StreamingFetcher = (*DiskCache)(nil)
        _ blobref.SeekFetcher      = (*DiskCache)(nil)
)

I understand that no variables are created and the statements ensure compiler checks that CachingFether implements public functions of StreamingFetcher and SeekFetcher. RHS portion uses a pointer constructor syntax with a nil parameter. What does this syntax mean in Go language ?

解决方案

(*T)(nil) is a Conversion. In this case it stands for a typed nil, ie. the same value which, for example

var p *T

has before assigning anything to it.

The standard syntax of a conversion is T(expr), but the priority of the * would bind it wrongly in

*T(expr)

This syntax means dereferencing the return value of function T with one argument expr. That's why the conversion has an alternative syntax:

(T)(expr)

where T can of course be *U. Therefore

(*U)(expr)

is the generalized form of what you see in the Camlistore repository.

这篇关于golang接口符合性编译类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆