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

查看:20
本文介绍了确保一个类型在 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天全站免登陆