{"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":[],"categories":[122],"tags":[231,232],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.6.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Go: Interface (example) &ndash; 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) &ndash; 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=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:site\" content=\"@MthsBk\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"1 minute\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/denbeke.be\/blog\/#website\",\"url\":\"https:\/\/denbeke.be\/blog\/\",\"name\":\"DenBeke\",\"description\":\"Mathias Beke\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/denbeke.be\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/#webpage\",\"url\":\"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/\",\"name\":\"Go: Interface (example) &ndash; 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\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/denbeke.be\/blog\/programmeren\/go-interface-example\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/denbeke.be\/blog\/#\/schema\/person\/386878f712fe3fe22227216f087772dc\",\"name\":\"Mathias Beke\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/denbeke.be\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/015ba35e6ce4f5859e3888ca99807575?s=96&d=mm&r=g\",\"caption\":\"Mathias Beke\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/denbeke.be\/blog\/wp-json\/wp\/v2\/posts\/1679"}],"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}]}}