确保类型在Go中的编译时实现接口 [英] Ensure a type implements an interface at compile time in Go

查看:62
本文介绍了确保类型在Go中的编译时实现接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确保类型在编译时实现接口?这样做的典型方法是无法从该类型分配支持接口,但是我有几种类型只能动态转换。在运行时,这会生成非常粗糙的错误消息,而没有给出针对编译时错误的更好诊断。

How can I ensure that a type implements an interface at compile time? The typical way to do this is by failure to assign to support interfaces from that type, however I have several types that are only converted dynamically. At runtime this generates very gruff error messages, without the better diagnostics given for compile time errors. It's also very inconvenient to find at run time that types I expected to support interfaces, do in fact not.

推荐答案

假设问题出在运行时发现我期望支持接口的类型,实际上却不支持,这也非常不便。与Go有关,例如

Assuming the question is about Go, e.g.

var _ foo.RequiredInterface = myType{} // or &myType{} or [&]myType if scalar

作为TLD将在编译时为您检查。

as a TLD will check that for you at compile time.

这篇关于确保类型在Go中的编译时实现接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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