{"id":4273,"date":"2014-03-30T09:36:55","date_gmt":"2014-03-30T09:36:55","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/ensure-a-type-implements-an-interface-at-compile-time-in-go-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:36:55","modified_gmt":"2014-03-30T09:36:55","slug":"ensure-a-type-implements-an-interface-at-compile-time-in-go-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/ensure-a-type-implements-an-interface-at-compile-time-in-go-collection-of-common-programming-errors\/","title":{"rendered":"Ensure a type implements an interface at compile time in Go-Collection of common programming errors"},"content":{"rendered":"<p>I don&#8217;t like the idea of making compiler throw errors by putting dummy lines in the main code. That&#8217;s a smart solution that works, but I prefer to write a test for this purpose.<\/p>\n<p>Assuming that we have:<\/p>\n<pre><code>type Intfc interface { Func() }\ntype Typ int\nfunc (t Typ) Func() {}\n<\/code><\/pre>\n<p>This test makes sure <code>Typ<\/code> implements <code>Intfc<\/code>:<\/p>\n<pre><code>package main\n\nimport (\n    \"reflect\"\n    \"testing\"\n)\n\nfunc TestTypes(t *testing.T) {\n    var interfaces struct {\n        intfc Intfc\n    }\n    var typ Typ\n    v := reflect.ValueOf(interfaces)\n    testType(t, reflect.TypeOf(typ), v.Field(0).Type())\n}\n\n\/\/ testType checks if type t1 implements interface t2\nfunc testType(t *testing.T, t1, t2 reflect.Type) {\n    if !t1.Implements(t2) {\n        t.Errorf(\"%v does not implement %v\", t1, t2)\n    }\n}\n<\/code><\/pre>\n<p>You can check all of your types and interfaces by adding them to <code>TestTypes<\/code> function. Writing tests for Go is introduced here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I don&#8217;t like the idea of making compiler throw errors by putting dummy lines in the main code. That&#8217;s a smart solution that works, but I prefer to write a test for this purpose. Assuming that we have: type Intfc interface { Func() } type Typ int func (t Typ) Func() {} This test makes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4273","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4273","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4273"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4273\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4273"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4273"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4273"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}