{"id":796,"date":"2019-12-01T13:52:08","date_gmt":"2019-12-01T13:52:08","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=796"},"modified":"2019-12-01T13:52:17","modified_gmt":"2019-12-01T13:52:17","slug":"go-check-if-type-implements-interface","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/","title":{"rendered":"Go : Check if type implements an interface"},"content":{"rendered":"\n

Sometimes there can be scenarios where it is needed to know if your type satisfies an interface or not. This can be easily achieved using a blank identifier.<\/p>\n\n\n\n

package main\n\ntype shape interface {\n    getNumSides() int\n    getArea() int\n}\n\ntype square struct {\n    len int\n}\n\nfunc (s square) getNumSides() int {\n    return 4\n}\n\n\/\/ func (s square) getArea() int {\n\/\/  return s.len * 2\n\/\/ }\n\nfunc main() {\n    \/\/Verify that *square implement shape\n    var _ shape = square{}\n}<\/code><\/pre>\n\n\n\n

In the above program, we have commented out the square’s getArea() <\/strong>function. On “go build” above program gives the error<\/p>\n\n\n\n

main.go:25:6: cannot use square literal (type square) as type shape in assignment:\n        square does not implement shape (missing getArea method)<\/code><\/pre>\n\n\n\n

On uncommenting the getArea() <\/strong>method the error goes away. To check if a pointer of the type implements the interface then it can be done as below<\/p>\n\n\n\n

var _ shape = &square{}<\/code><\/pre>\n\n\n\n

OR<\/strong><\/p>\n\n\n\n

var _ shape = (*square)(nil)<\/code><\/pre>\n\n\n\n

First one will give the error <\/p>\n\n\n\n

main.go:25:6: cannot use (*square)(nil) (type *square) as type shape in assignment:\n        *square does not implement shape (missing getArea method)<\/code><\/pre>\n\n\n\n

while the latter one will give error <\/p>\n\n\n\n

main.go:25:6: cannot use &square literal (type *square) as type shape in assignment:\n        *square does not implement shape (missing getArea method)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Sometimes there can be scenarios where it is needed to know if your type satisfies an interface or not. This can be easily achieved using a blank identifier. In the above program,…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,11,99],"class_list":["post-796","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-interface","tag-type"],"yoast_head":"\nGo : Check if type implements an interface - Welcome To Golang By Example<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go : Check if type implements an interface - Welcome To Golang By Example\" \/>\n<meta property=\"og:description\" content=\"Sometimes there can be scenarios where it is needed to know if your type satisfies an interface or not. This can be easily achieved using a blank identifier. In the above program,...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/\" \/>\n<meta property=\"og:site_name\" content=\"Welcome To Golang By Example\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-01T13:52:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-01T13:52:17+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/\",\"url\":\"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/\",\"name\":\"Go : Check if type implements an interface - Welcome To Golang By Example\",\"isPartOf\":{\"@id\":\"https:\/\/golangbyexamples.com\/#website\"},\"datePublished\":\"2019-12-01T13:52:08+00:00\",\"dateModified\":\"2019-12-01T13:52:17+00:00\",\"author\":{\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\"},\"breadcrumb\":{\"@id\":\"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/golangbyexamples.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go : Check if type implements an interface\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/golangbyexamples.com\/#website\",\"url\":\"https:\/\/golangbyexamples.com\/\",\"name\":\"Welcome To Golang By Example\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/golangbyexamples.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"url\":\"https:\/\/golangbyexamples.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go : Check if type implements an interface - Welcome To Golang By Example","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/","og_locale":"en_US","og_type":"article","og_title":"Go : Check if type implements an interface - Welcome To Golang By Example","og_description":"Sometimes there can be scenarios where it is needed to know if your type satisfies an interface or not. This can be easily achieved using a blank identifier. In the above program,...","og_url":"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/","og_site_name":"Welcome To Golang By Example","article_published_time":"2019-12-01T13:52:08+00:00","article_modified_time":"2019-12-01T13:52:17+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/","url":"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/","name":"Go : Check if type implements an interface - Welcome To Golang By Example","isPartOf":{"@id":"https:\/\/golangbyexamples.com\/#website"},"datePublished":"2019-12-01T13:52:08+00:00","dateModified":"2019-12-01T13:52:17+00:00","author":{"@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f"},"breadcrumb":{"@id":"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/golangbyexamples.com\/go-check-if-type-implements-interface\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/golangbyexamples.com\/"},{"@type":"ListItem","position":2,"name":"Go : Check if type implements an interface"}]},{"@type":"WebSite","@id":"https:\/\/golangbyexamples.com\/#website","url":"https:\/\/golangbyexamples.com\/","name":"Welcome To Golang By Example","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/golangbyexamples.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/8833ea7638dafd763cb1db6c0ca4576f","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/golangbyexamples.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"admin"},"url":"https:\/\/golangbyexamples.com\/author\/admin\/"}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paOs1b-cQ","amp_validity":null,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/comments?post=796"}],"version-history":[{"count":1,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/796\/revisions"}],"predecessor-version":[{"id":797,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/posts\/796\/revisions\/797"}],"wp:attachment":[{"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/media?parent=796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/categories?post=796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/golangbyexamples.com\/wp-json\/wp\/v2\/tags?post=796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}