{"id":1679,"date":"2014-10-30T19:24:50","date_gmt":"2014-10-30T18:24:50","guid":{"rendered":"https:\/\/denbeke.be\/blog\/?p=1679"},"modified":"2014-10-30T20:31:12","modified_gmt":"2014-10-30T19:31:12","slug":"go-interface-example","status":"publish","type":"post","link":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/","title":{"rendered":"Go: Interface (example)"},"content":{"rendered":"<p>An Interface in Go defines a behavior. Interfaces are defined with a number of methods that a type must implement. Once the methods are implemented, the type can be used wherever the interface type is needed.<\/p>\n<p>Let&#8217;s for example\u00a0define a <code>Phone<\/code> interface:<\/p>\n<pre><code>type Phone interface {\r\n\tCall()\r\n}<\/code><\/pre>\n<p>We also define a <code>Nokia<\/code> type that implements the Phone interface. We simply need to implement the <code>Call()<\/code> function to implement the <code>Phone<\/code> interface. Languages such as Java or C# need an explicit statement that they are implementing the interface: <code>public class Nokia implements Phone { }<\/code>. In Go you just need to implement the functions:<\/p>\n<pre><code class=\"golang\">type Nokia struct {\r\n    \/\/...\r\n}\r\n\r\nfunc (n Nokia) Call() {\r\n    fmt.Println(\"Calling someone with my Nokia\")\r\n}<\/code><\/pre>\n<p>Besides the <code>Nokia<\/code> we also want an <code>Iphone<\/code>:<\/p>\n<pre><code>type Iphone struct {\r\n    \/\/...\r\n}\r\n\r\nfunc (n Iphone) Call() {\r\n    fmt.Println(\"Calling someone with my iPhone\")\r\n}<\/code><\/pre>\n<p>Now we define a more generic <code>UseMyPhone<\/code> function that will use the <code>Phone<\/code> interface:<\/p>\n<pre><code class=\"golang\">func UseMyPhone(p Phone) {\r\n    p.Call()\r\n}<\/code><\/pre>\n<p>This allows us to call the <code>UseMyPhone<\/code> function with any <code>Phone<\/code> type:<\/p>\n<pre><code>n := Nokia{}\r\ni := Iphone{}\r\n\r\nUseMyPhone(n)\r\nUseMyPhone(i)<\/code><\/pre>\n<p><a title=\"Go Playground: Interface example\" href=\"http:\/\/play.golang.org\/p\/kAZxdsU_QC\" target=\"_blank\">Go Playground<\/a><\/p>\n<p>The ability to implement interfaces by just implementing the needed functions is called duck-typing (i.e. signature based polymorphism).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An Interface in Go defines a behavior. Interfaces are defined with a number of methods that a type must implement. Once the methods are implemented, the type can be used wherever the interface type is needed. Let&#8217;s for example\u00a0define a Phone interface: type Phone interface { Call() } We also define a Nokia type that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[122],"tags":[231,232],"class_list":["post-1679","post","type-post","status-publish","format-standard","hentry","category-programmeren","tag-go","tag-golang","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go: Interface (example) &#8211; DenBeke<\/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:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go: Interface (example) &#8211; DenBeke\" \/>\n<meta property=\"og:description\" content=\"An Interface in Go defines a behavior. Interfaces are defined with a number of methods that a type must implement. Once the methods are implemented, the type can be used wherever the interface type is needed. Let&#8217;s for example\u00a0define a Phone interface: type Phone interface { Call() } We also define a Nokia type that [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/\" \/>\n<meta property=\"og:site_name\" content=\"DenBeke\" \/>\n<meta property=\"article:published_time\" content=\"2014-10-30T18:24:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-10-30T19:31:12+00:00\" \/>\n<meta name=\"author\" content=\"Mathias Beke\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mathias Beke\" \/>\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\":\"Article\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/\"},\"author\":{\"name\":\"Mathias Beke\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\"},\"headline\":\"Go: Interface (example)\",\"datePublished\":\"2014-10-30T18:24:50+00:00\",\"dateModified\":\"2014-10-30T19:31:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/\"},\"wordCount\":141,\"commentCount\":0,\"keywords\":[\"go\",\"golang\"],\"articleSection\":[\"Programmeren\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/\",\"name\":\"Go: Interface (example) &#8211; DenBeke\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\"},\"datePublished\":\"2014-10-30T18:24:50+00:00\",\"dateModified\":\"2014-10-30T19:31:12+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/programmeren\\\/go-interface-example\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go: Interface (example)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/denbeke.be\\\/blog\\\/#\\\/schema\\\/person\\\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go: Interface (example) &#8211; DenBeke","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:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/","og_locale":"en_US","og_type":"article","og_title":"Go: Interface (example) &#8211; DenBeke","og_description":"An Interface in Go defines a behavior. Interfaces are defined with a number of methods that a type must implement. Once the methods are implemented, the type can be used wherever the interface type is needed. Let&#8217;s for example\u00a0define a Phone interface: type Phone interface { Call() } We also define a Nokia type that [&hellip;]","og_url":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/","og_site_name":"DenBeke","article_published_time":"2014-10-30T18:24:50+00:00","article_modified_time":"2014-10-30T19:31:12+00:00","author":"Mathias Beke","twitter_card":"summary_large_image","twitter_creator":"@MthsBk","twitter_site":"@MthsBk","twitter_misc":{"Written by":"Mathias Beke","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/#article","isPartOf":{"@id":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/"},"author":{"name":"Mathias Beke","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc"},"headline":"Go: Interface (example)","datePublished":"2014-10-30T18:24:50+00:00","dateModified":"2014-10-30T19:31:12+00:00","mainEntityOfPage":{"@id":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/"},"wordCount":141,"commentCount":0,"keywords":["go","golang"],"articleSection":["Programmeren"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/","url":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/","name":"Go: Interface (example) &#8211; DenBeke","isPartOf":{"@id":"https:\/\/denbeke.be\/blog\/#website"},"datePublished":"2014-10-30T18:24:50+00:00","dateModified":"2014-10-30T19:31:12+00:00","author":{"@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc"},"breadcrumb":{"@id":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/denbeke.be\/blog\/"},{"@type":"ListItem","position":2,"name":"Go: Interface (example)"}]},{"@type":"WebSite","@id":"https:\/\/denbeke.be\/blog\/#website","url":"https:\/\/denbeke.be\/blog\/","name":"DenBeke","description":"Mathias Beke","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/denbeke.be\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc","name":"Mathias Beke","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8889cf00cef9570a8f82e7510ed97205ccb7a1a0d2c7e0968a07e1885386f198?s=96&d=mm&r=g","caption":"Mathias Beke"}}]}},"_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/comments?post=1679"}],"version-history":[{"count":12,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1679\/revisions"}],"predecessor-version":[{"id":1691,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1679\/revisions\/1691"}],"wp:attachment":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/media?parent=1679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/categories?post=1679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/tags?post=1679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}