{"id":1234,"date":"2021-01-12T09:26:04","date_gmt":"2021-01-12T09:26:04","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=1234"},"modified":"2021-01-12T09:38:26","modified_gmt":"2021-01-12T09:38:26","slug":"golang-interfaces","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2021\/01\/12\/golang-interfaces\/","title":{"rendered":"GOLang: Interfaces"},"content":{"rendered":"\n<p class=\"has-drop-cap\">Interfaces are an interesting concept to learn in GOLANG. There are a tonne of informative articles and here is my contribution to it.<\/p>\n\n\n\n<p>In classical Object Oriented everything is treated in parlance of <code>Object<\/code> or <code>Class<\/code> like in <code>Java<\/code> or <code>C++<\/code>. In Go there is no class or inheritance. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Learn by doing<\/h2>\n\n\n\n<p>I started of by defining an interface <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>type MyInterface interface {\n\tSet(int)\n\tGet() int\n}<\/code><\/pre>\n\n\n\n<p>Which exposes two methods <code>Get<\/code> and <code>Set<\/code> and any user defined object that implements it (as shown below) can be assigned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What it represents?<\/h2>\n\n\n\n<p>Let&#8217;s try and print the value and type for a var of type <code>MyInterface<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%#v\ta Go-syntax representation of the value\n%T\ta Go-syntax representation of the type of the value\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>var myIntf MyInterface\nfmt.Printf(\" Before assigning the object %T %#v\\n\", myIntf, myIntf)<\/code><\/pre>\n\n\n\n<p>The output might surprise you &#8211; for those coming from the Java background.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Before assigning the object &lt;nil&gt; &lt;nil&gt;<\/code><\/pre>\n\n\n\n<p>The <code>var myIntf<\/code> is essentially a pointer with a value of <code>nil<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>There is no explicit <code>implements<\/code> like verb in <code>go<\/code><\/li><li>The interface can be implemented by any number of <code>types<\/code><\/li><li>Multiple interfaces can be implemented by the same <code>type<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Full Program<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport \"fmt\"\n\n\/\/ MyInterface defines simple set and get\ntype MyInterface interface {\n\tSet(int)\n\tGet() int\n}\n\n\/\/ Simple Struct that has only a single Value\ntype Simple struct {\n\tu int\n}\n\n\/\/ Set the value for Simple struct\nfunc (p *Simple) Set(u int) {\n\tp.u = u\n}\n\n\/\/Get returns the value in struct\nfunc (p Simple) Get() int {\n\treturn p.u\n}\n\nfunc main() {\n\tsimple := &amp;Simple{u: 20}\n\tsimple2 := Simple{u: 21}\n\n\tfmt.Printf(\" Value: %T %#v\\n\", simple, simple)\n\tfmt.Printf(\" Value: %d\\n\", simple.Get())\n\n\tvar myIntf MyInterface\n\tfmt.Printf(\" Before assigning the object %T %#v\\n\", myIntf, myIntf)\n\n\tmyIntf = simple\n\tmyIntf.Set(22)\n\tfmt.Printf(\" Value: %T %#v\\n\", myIntf, myIntf)\n\tfmt.Printf(\" Value: %d\\n\", myIntf.Get())\n\n\tmyIntf = &amp;simple2\n\tmyIntf.Set(24)\n\tfmt.Printf(\" Value: %T %#v\\n\", myIntf, myIntf)\n\tfmt.Printf(\" Value: %d\\n\", myIntf.Get())\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>API server listening at: 127.0.0.1:19141\n Value: *main.Simple &amp;main.Simple{u:20}\n Value: 20\n Before assigning the object &lt;nil&gt; &lt;nil&gt;\n Value: *main.Simple &amp;main.Simple{u:22}\n Value: 22\n Value: *main.Simple &amp;main.Simple{u:24}\n Value: 24<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Multiple Interfaces<\/h2>\n\n\n\n<p>How about adding another interface<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Print can do anything\ntype Print interface {\n\tPrint()\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Print Wildcard print\nfunc (p *Simple) Print() {\n\tfmt.Printf(\"Type: %T\\nValue: %+v\\n\", p, p)\n}<\/code><\/pre>\n\n\n\n<p>I modified the main code like below to invoke print.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func main() {\n\tsimple := &amp;Simple{u: 20}\n\tsimple2 := Simple{u: 21}\n\n\tfmt.Printf(\" Value: %T %#v\\n\", simple, simple)\n\tfmt.Printf(\" Value: %d\\n\", simple.Get())\n\tsimple.Print()\n\n\tvar myIntf MyInterface\n\tfmt.Printf(\" Before assigning the object %T %#v\\n\", myIntf, myIntf)\n\n\tmyIntf = simple\n\tmyIntf.Set(22)\n\tfmt.Printf(\" Value: %T %#v\\n\", myIntf, myIntf)\n\tfmt.Printf(\" Value: %d\\n\", myIntf.Get())\n\tvar print Print\n\tprint = simple\n\tprint.Print()\n\n\tmyIntf = &amp;simple2\n\tmyIntf.Set(24)\n\tfmt.Printf(\" Value: %T %#v\\n\", myIntf, myIntf)\n\tfmt.Printf(\" Value: %d\\n\", myIntf.Get())\n\n\tprint = &amp;simple2\n\tprint.Print()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Output<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>API server listening at: 127.0.0.1:36898\n Value: *main.Simple &amp;main.Simple{u:20}\n Value: 20\nType: *main.Simple\nValue: &amp;{u:20}\n Before assigning the object &lt;nil> &lt;nil>\n Value: *main.Simple &amp;main.Simple{u:22}\n Value: 22\nType: *main.Simple\nValue: &amp;{u:22}\n Value: *main.Simple &amp;main.Simple{u:24}\n Value: 24\nType: *main.Simple\nValue: &amp;{u:24}<\/code><\/pre>\n\n\n\n<p>Or, adding another type<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/Age struct a siple numberic holder\ntype Age struct {\n\ta myInt\n}\n\n\/\/Print Wildcard print\nfunc (p *Age) Print() {\n\tfmt.Printf(\"Type: %T\\nValue: %+v\\n\", p, p)\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>age := &amp;Age{a: 40}\nage.Print()\nprint = age\nprint.Print()<\/code><\/pre>\n\n\n\n<p>Age only implements <code>Print<\/code>, so in case if we try to use it with the type <code>MyInterface<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"130\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-12-at-3.06.31-PM-1024x130.png\" alt=\"\" class=\"wp-image-1241\" srcset=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-12-at-3.06.31-PM-1024x130.png 1024w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-12-at-3.06.31-PM-300x38.png 300w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-12-at-3.06.31-PM-768x98.png 768w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-12-at-3.06.31-PM-1536x195.png 1536w, https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2021\/01\/Screenshot-2021-01-12-at-3.06.31-PM.png 1794w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>Value: &amp;{u:24}\nType: *main.Age\nValue: &amp;{a:40}\nType: *main.Age\nValue: &amp;{a:40}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Interfaces are an interesting concept to learn in GOLANG. There are a tonne of informative articles and here is my contribution to it. In classical Object Oriented everything is treated in parlance of Object or Class like in Java or C++. In Go there is no class or inheritance. Learn by doing I started of [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1236,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"image","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[137,34],"tags":[23,138],"class_list":["post-1234","post","type-post","status-publish","format-image","has-post-thumbnail","hentry","category-golang","category-technical","tag-golang","tag-interfaces","post_format-post-format-image"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=1234"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/1234\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/1236"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=1234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=1234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=1234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}