{"id":340,"date":"2020-01-26T11:13:24","date_gmt":"2020-01-26T11:13:24","guid":{"rendered":"https:\/\/www.samarthya.me\/wps\/?p=340"},"modified":"2020-02-01T13:24:39","modified_gmt":"2020-02-01T13:24:39","slug":"nuil-intf","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/01\/26\/nuil-intf\/","title":{"rendered":"Nil Interface &#8211; GoLang"},"content":{"rendered":"\n<p>An interface in Golang is a set of methods and a type.<\/p>\n\n\n<div>\n<div>\n<div>What is the purpose of Nil interface in GO-lang<\/div>\n<\/div>\n<\/div>\n<div class=\"slide-content ng-binding\">\n<blockquote>\n<p>A nil interface value holds neither value nor concrete type.<\/p>\n<\/blockquote>\n<\/div>\n\n\n<pre class=\"wp-block-preformatted\">type NilI interface {\n     NoFunction()\n}<\/pre>\n\n\n\n<p>Just defined an interface with a simple function <code>NoFunction<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">func main() {\n    NilI i\n    i.NoFunction()\n}<\/pre>\n\n\n\n<p>Executing it results in<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">panic: runtime error: invalid memory address or nil pointer dereference<\/pre>\n\n\n\n<p>On the same lines there is <code>empty<\/code> interface, but that can hold any value; but what is nil interface suppose to do?<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">interface{}<\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>An empty interface that specifies zero methods.<\/p><\/blockquote>\n\n\n\n<p>An empty interface is used for unknown types.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">e.g \nfunc Printf(format <a href=\"https:\/\/golang.org\/pkg\/builtin\/#string\">string<\/a>, a ...interface{}) (n <a href=\"https:\/\/golang.org\/pkg\/builtin\/#int\">int<\/a>, err <a href=\"https:\/\/golang.org\/pkg\/builtin\/#error\">error<\/a>)\n\nvar i interface{}\nfmt.Printf(\"(%v, %T)\\n\", i, i)\n\nOutputs\n(&lt;nil&gt;, &lt;nil&gt;)\n\nif, we assign \ni = 42\n\nOutputs\n(42, int)<\/pre>\n\n\n\n<p>What if you want to evaluate the type of the object or interface? <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Type Assertion<\/h2>\n\n\n\n<p>A <em>type assertion<\/em> provides access to an interface value&#8217;s underlying concrete value.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>t.(type)<\/p><\/blockquote>\n\n\n\n<h4 class=\"has-text-align-center wp-block-heading\">type, identifies the concrete value such as int, float etc.<\/h4>\n\n\n\n<p>The statement asserts the t holds the concrete type <code>type<\/code>. If t does not holds Type <code>type<\/code> it will throw a panic error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">another syntactical sugar\ns, ok := t.(type)<\/pre>\n\n\n\n<p>If t has a Type <code>type<\/code> then s will have the concrete value and <code>ok<\/code> will have value true, else <code>ok<\/code> will be false and <code>s<\/code> zero value of Type <code>type<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">e.g.\nvar i interface{} = \"simple\"\n\ns := i.(string)\nfmt.Println(s)\n\nOutputs:\nsimple\n\ns, ok := i.(string)\nfmt.Println(s, ok)\n\nOutputs:\nsimple true\n\nf, ok := i.(int)\nfmt.Println(f, ok)\n\nOutputs:\npanic: interface conversion: interface {} is string, not float64<\/pre>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">&#8212; THE &#8211; END &#8212;<\/h3>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An interface in Golang is a set of methods and a type. What is the purpose of Nil interface in GO-lang A nil interface value holds neither value nor concrete type. type NilI interface { NoFunction() } Just defined an interface with a simple function NoFunction func main() { NilI i i.NoFunction() } Executing it [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":342,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[1,34],"tags":[43,44],"class_list":["post-340","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-others","category-technical","tag-interface","tag-nil"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/340","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=340"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/342"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}