{"id":1080,"date":"2026-08-17T00:27:46","date_gmt":"2026-08-17T00:27:46","guid":{"rendered":"https:\/\/www.devopsschool.com\/tutorials\/?p=1080"},"modified":"2026-08-17T00:27:47","modified_gmt":"2026-08-17T00:27:47","slug":"go-tutorials-go-interfaces-implicit-interface-implementation","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/tutorials\/go-tutorials-go-interfaces-implicit-interface-implementation\/","title":{"rendered":"Go Tutorials: Go Interfaces &amp; Implicit Interface Implementation"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">These two ideas are closely related. The easiest mental model is:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>Interface = a list of behaviors.<\/strong><br><strong>Implicit implementation = Go automatically accepts any type that has those behaviors.<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">You never write <code>implements<\/code> in Go.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">1. Interfaces<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">What is an interface?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An <strong>interface<\/strong> describes <strong>what something can do<\/strong>, without saying <strong>how it does it<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Speaker interface {\n    Speak() string\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This means:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">Anything that has a method <code>Speak() string<\/code> can be treated as a <code>Speaker<\/code>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">The interface does <strong>not<\/strong> contain the implementation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why use interfaces?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Interfaces help you write code that works with <strong>different types<\/strong> without caring about their exact type.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dog can speak<\/li>\n\n\n\n<li>Person can speak<\/li>\n\n\n\n<li>Robot can speak<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Your program doesn&#8217;t necessarily need to know whether something is a Dog, Person, or Robot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It only needs to know:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">&#8220;Can this thing <code>Speak()<\/code>?&#8221;<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Complete Example \u2014 Interface<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Copy and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Speaker describes a behavior.\n\/\/\n\/\/ Any type that has:\n\/\/     Speak() string\n\/\/\n\/\/ can be used as a Speaker.\ntype Speaker interface {\n\tSpeak() string\n}\n\n\/\/ Dog is a normal struct.\ntype Dog struct {\n\tName string\n}\n\n\/\/ Dog has the Speak method.\nfunc (d Dog) Speak() string {\n\treturn d.Name + \" says Woof!\"\n}\n\n\/\/ Person is another normal struct.\ntype Person struct {\n\tName string\n}\n\n\/\/ Person also has the Speak method.\nfunc (p Person) Speak() string {\n\treturn p.Name + \" says Hello!\"\n}\n\n\/\/ This function does NOT care\n\/\/ whether it receives Dog, Person, etc.\n\/\/\n\/\/ It only requires something\n\/\/ that satisfies the Speaker interface.\nfunc makeItSpeak(s Speaker) {\n\tfmt.Println(s.Speak())\n}\n\nfunc main() {\n\tdog := Dog{Name: \"Buddy\"}\n\tperson := Person{Name: \"John\"}\n\n\tmakeItSpeak(dog)\n\tmakeItSpeak(person)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Buddy says Woof!\nJohn says Hello!<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Understand the important part<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Look at this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Speaker interface {\n\tSpeak() string\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The interface says:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>I need something with Speak() string<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (d Dog) Speak() string<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Dog has that method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (p Person) Speak() string<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Person also has that method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore both can be passed here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func makeItSpeak(s Speaker)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What happened?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Conceptually:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>flowchart TD\n    A&#91;\"Speaker interface\"] --&gt; B&#91;\"Requires: Speak() string\"]\n    C&#91;\"Dog\"] --&gt; D&#91;\"Has Speak() string\"]\n    E&#91;\"Person\"] --&gt; F&#91;\"Has Speak() string\"]\n    D --&gt; G&#91;\"Can be used as Speaker\"]\n    F --&gt; G<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why is this useful?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Without the interface, you might write:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func makeDogSpeak(d Dog) {\n\tfmt.Println(d.Speak())\n}\n\nfunc makePersonSpeak(p Person) {\n\tfmt.Println(p.Speak())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With an interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func makeItSpeak(s Speaker) {\n\tfmt.Println(s.Speak())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">One function works with many different types.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">When should I use an interface?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Use an interface when several types should be usable through the <strong>same behavior<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Writer\n    Write()\n\nReader\n    Read()\n\nNotifier\n    Notify()\n\nPaymentProcessor\n    Pay()\n\nStorage\n    Save()\n\nSpeaker\n    Speak()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Think:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">&#8220;I don&#8217;t care exactly WHAT you are. I care about WHAT YOU CAN DO.&#8221;<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Where are interfaces commonly used?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll see them in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>function parameters<\/li>\n\n\n\n<li>testing and mocks<\/li>\n\n\n\n<li>database abstractions<\/li>\n\n\n\n<li>HTTP code<\/li>\n\n\n\n<li>file handling<\/li>\n\n\n\n<li>logging<\/li>\n\n\n\n<li>external services<\/li>\n\n\n\n<li>application architecture<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For a beginner, the most important use is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func doSomething(x SomeInterface)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It allows different types to be passed to the same function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">2. Implicit Interface Implementation<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">This is one of the most important differences between Go and languages such as Java.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Go, you <strong>do not explicitly declare that a type implements an interface<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go checks the methods automatically.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The key rule<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Notifier interface {\n\tNotify()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Email struct{}\n\nfunc (e Email) Notify() {\n\tfmt.Println(\"Sending email\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go automatically knows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Email has Notify()\n\nNotifier requires Notify()\n\nTherefore:\n\nEmail satisfies Notifier<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There is no:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>implements Notifier<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That is why it is called <strong>implicit interface implementation<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Complete Example \u2014 Implicit Interface Implementation<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Copy and run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ Notifier says:\n\/\/ \"Any type with Notify() can be a Notifier.\"\ntype Notifier interface {\n\tNotify()\n}\n\n\/\/ Email is just a normal struct.\ntype Email struct {\n\tAddress string\n}\n\n\/\/ Email has the method required by Notifier.\n\/\/\n\/\/ Notice:\n\/\/ We NEVER wrote:\n\/\/     Email implements Notifier\n\/\/\n\/\/ Go figures it out automatically.\nfunc (e Email) Notify() {\n\tfmt.Println(\"Sending email to:\", e.Address)\n}\n\n\/\/ SMS is another normal struct.\ntype SMS struct {\n\tPhone string\n}\n\n\/\/ SMS also has Notify().\n\/\/\n\/\/ Therefore SMS ALSO automatically\n\/\/ satisfies the Notifier interface.\nfunc (s SMS) Notify() {\n\tfmt.Println(\"Sending SMS to:\", s.Phone)\n}\n\n\/\/ sendNotification accepts ANY type\n\/\/ that satisfies Notifier.\nfunc sendNotification(n Notifier) {\n\tn.Notify()\n}\n\nfunc main() {\n\temail := Email{\n\t\tAddress: \"john@example.com\",\n\t}\n\n\tsms := SMS{\n\t\tPhone: \"1234567890\",\n\t}\n\n\tsendNotification(email)\n\tsendNotification(sms)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sending email to: john@example.com\nSending SMS to: 1234567890<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">What exactly makes <code>Email<\/code> a <code>Notifier<\/code>?<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">The interface requires:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Notifier interface {\n\tNotify()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Email has:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (e Email) Notify()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So Go says:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Notifier requirement\n        \u2193\n    Notify()\n        \u2191\nEmail has Notify()\n        \u2193\nEmail satisfies Notifier<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Nothing else is required.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">There is no <code>implements<\/code><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">This is the part worth remembering.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In some languages you might see something conceptually like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Email implements Notifier<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Go does <strong>not<\/strong> work like that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You simply write:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Email struct{}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (e Email) Notify() {\n\tfmt.Println(\"Email sent\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s enough.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go notices that the method matches the interface.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">How does Go decide?<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Go compares the interface&#8217;s required methods with the type&#8217;s methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Notifier interface {\n\tNotify()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Type A:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Email struct{}\n\nfunc (Email) Notify() {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Implements <code>Notifier<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Type B:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type SMS struct{}\n\nfunc (SMS) Notify() {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Implements <code>Notifier<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Type C:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Car struct{}\n\nfunc (Car) Drive() {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u274c Does NOT implement <code>Notifier<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because <code>Car<\/code> has:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Drive()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">but the interface requires:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Notify()<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">What if an interface has multiple methods?<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Then the type must have <strong>all<\/strong> of them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Device interface {\n\tStart()\n\tStop()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This satisfies it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Computer struct{}\n\nfunc (Computer) Start() {}\nfunc (Computer) Stop() {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">But this does not:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Computer struct{}\n\nfunc (Computer) Start() {}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">because:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Start() \u2705\nStop()  \u274c<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All required methods must exist.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Interfaces vs Implicit Implementation<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Concept<\/th><th>Interface<\/th><th>Implicit Implementation<\/th><\/tr><\/thead><tbody><tr><td>What is it?<\/td><td>Defines required behaviors<\/td><td>Determines whether a type satisfies an interface<\/td><\/tr><tr><td>Example<\/td><td><code>type Speaker interface { Speak() }<\/code><\/td><td><code>Dog<\/code> has <code>Speak()<\/code>, so Dog satisfies Speaker<\/td><\/tr><tr><td>Contains data?<\/td><td>No<\/td><td>Not applicable<\/td><\/tr><tr><td>Contains required methods?<\/td><td>Yes<\/td><td>Type must provide those methods<\/td><\/tr><tr><td>Need <code>implements<\/code> keyword?<\/td><td>No<\/td><td>No<\/td><\/tr><tr><td>Checked by Go?<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Main purpose<\/td><td>Define behavior<\/td><td>Connect types to interfaces automatically<\/td><\/tr><tr><td>Think of it as<\/td><td>&#8220;What must you do?&#8221;<\/td><td>&#8220;You can already do it, so you qualify.&#8221;<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">Interface vs Struct<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">This distinction is also important.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Struct<\/th><th>Interface<\/th><\/tr><\/thead><tbody><tr><td>Describes data<\/td><td>Describes behavior<\/td><\/tr><tr><td>Contains fields<\/td><td>Contains method requirements<\/td><\/tr><tr><td><code>Name string<\/code><\/td><td><code>Speak() string<\/code><\/td><\/tr><tr><td>Represents what something <strong>is<\/strong><\/td><td>Represents what something <strong>can do<\/strong><\/td><\/tr><tr><td>Usually creates concrete values<\/td><td>Usually used to accept multiple types<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Example struct:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Dog struct {\n\tName string\n\tAge  int\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This describes data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example interface:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type Speaker interface {\n\tSpeak() string\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This describes behavior.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">The relationship between everything<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>flowchart TD\n    A&#91;\"Interface\"] --&gt; B&#91;\"Defines required methods\"]\n\n    B --&gt; C&#91;\"Speak() string\"]\n\n    D&#91;\"Dog\"] --&gt; E&#91;\"Has Speak() string\"]\n    F&#91;\"Person\"] --&gt; G&#91;\"Has Speak() string\"]\n    H&#91;\"Car\"] --&gt; I&#91;\"Has Drive()\"]\n\n    E --&gt; J&#91;\"Satisfies Speaker\"]\n    G --&gt; J\n    I --&gt; K&#91;\"Does NOT satisfy Speaker\"]\n\n    J --&gt; L&#91;\"Can be passed to function expecting Speaker\"]<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">The Mental Model<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Don&#8217;t think:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dog IS a Speaker<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Instead think:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dog CAN Speak<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dog can be USED AS a Speaker.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That is very close to how Go interfaces are intended to be understood.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">What \u2192 Why \u2192 When \u2192 How \u2192 Where<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Question<\/th><th>Answer<\/th><\/tr><\/thead><tbody><tr><td><strong>What?<\/strong><\/td><td>An interface defines methods that a type must have<\/td><\/tr><tr><td><strong>Why?<\/strong><\/td><td>So one piece of code can work with many different types<\/td><\/tr><tr><td><strong>When?<\/strong><\/td><td>When different types share the same behavior<\/td><\/tr><tr><td><strong>How?<\/strong><\/td><td>Define an interface and give types matching methods<\/td><\/tr><tr><td><strong>Where?<\/strong><\/td><td>Commonly in function parameters, libraries, services, testing and abstractions<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For implicit implementation:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Question<\/th><th>Answer<\/th><\/tr><\/thead><tbody><tr><td><strong>What?<\/strong><\/td><td>Automatic interface satisfaction<\/td><\/tr><tr><td><strong>Why?<\/strong><\/td><td>Removes explicit <code>implements<\/code> declarations<\/td><\/tr><tr><td><strong>When?<\/strong><\/td><td>Whenever a type&#8217;s methods match an interface<\/td><\/tr><tr><td><strong>How?<\/strong><\/td><td>Simply implement all required methods<\/td><\/tr><tr><td><strong>Where?<\/strong><\/td><td>Everywhere interfaces are used in Go<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">The 5 Rules to Remember<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>An interface describes behavior.<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>type Speaker interface {\n\tSpeak()\n}<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>A type satisfies an interface by having all required methods.<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>func (Dog) Speak() {}<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>There is no <code>implements<\/code> keyword.<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">You never write:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dog implements Speaker<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Multiple unrelated types can satisfy the same interface.<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>Dog    \u2192 Speak()\nPerson \u2192 Speak()\nRobot  \u2192 Speak()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All three can satisfy <code>Speaker<\/code>.<\/p>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>Functions can accept the interface instead of the concrete type.<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>func talk(s Speaker) {\n\ts.Speak()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now <code>talk()<\/code> can work with every compatible type.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">One-Sentence Summary<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">The entire concept can be reduced to:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>An interface says &#8220;I need these methods&#8221;; any Go type that has those methods automatically satisfies the interface.<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">So whenever you see:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type X interface {\n\tDoSomething()\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">read it mentally as:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><strong>&#8220;Give me anything that knows how to <code>DoSomething()<\/code>.&#8221;<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">That mental model will make Go interfaces much easier to understand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>These two ideas are closely related. The easiest mental model is: Interface = a list of behaviors.Implicit implementation = Go automatically accepts any type that has those&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1080","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/1080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/comments?post=1080"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/1080\/revisions"}],"predecessor-version":[{"id":1081,"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/posts\/1080\/revisions\/1081"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/media?parent=1080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/categories?post=1080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/tutorials\/wp-json\/wp\/v2\/tags?post=1080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}