Ensure a type implements an interface at compile time in Go

后端 未结 6 1404
悲&欢浪女
悲&欢浪女 2020-11-30 08:51

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

6条回答
  •  执笔经年
    2020-11-30 09:23

    Assuming the question is about Go, e.g.

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

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

提交回复
热议问题