{"id":5047,"date":"2017-06-03T19:56:28","date_gmt":"2017-06-03T19:56:28","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5047"},"modified":"2021-07-16T21:01:35","modified_gmt":"2021-07-16T21:01:35","slug":"functions-and-methods-in-go","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/functions-and-methods-in-go\/","title":{"rendered":"Functions and Methods in Go"},"content":{"rendered":"<p>In the previous post on Go I&#8217;ve used the words method and function interchangeably, but as I learn more about Go I find that &#8211; in Go, methods and functions are actually different.<\/p>\n<p>First off, let me point you to this post <a href=\"https:\/\/golangbot.com\/methods\/\" target=\"_blank\" rel=\"noopener\">Part 17: Methods<\/a> which covers this subject in more depth. But here&#8217;s the elevator pitch.<\/p>\n<p><em>Methods names can be overloaded and the syntax of using a method is more like a method on an object.<\/em><\/p>\n<p>Let&#8217;s look at a simple example, a classic sort of OO example. We have different shape types and each has an area method.<\/p>\n<p><em>This is example is taken from <a href=\"https:\/\/golangbot.com\/methods\/\" target=\"_blank\" rel=\"noopener\">Part 17: Methods<\/a><\/em><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntype Rectangle struct {\r\n   length float64\r\n   width float64\r\n}\r\n\r\ntype Circle struct {\r\n   radius float64\r\n}\r\n\r\nfunc (shape Rectangle) Area() float64 {\r\n   return shape.length * shape.width\r\n}\r\n\r\nfunc (shape Circle) Area() float64 {\r\n   return math.Pi * math.Pow(shape.radius, 2)\r\n}\r\n<\/pre>\n<p>Notice that a method has the type declared before the method name (unlike a function) and this is because we call it in the way we would an extension method and object&#8217;s method in C#.<\/p>\n<p><em>In Go the type declared after the function name (function syntax) is known as an argument, whereas a type declared before the function name (method syntax) is known as a receiver.<\/em><\/p>\n<p>For example<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nr := Rectangle{ length = 100, width = 23 }\r\nc := Circle { radius = 30 }\r\n\r\nrArea := r.Area()\r\ncArea := c.Area()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the previous post on Go I&#8217;ve used the words method and function interchangeably, but as I learn more about Go I find that &#8211; in Go, methods and functions are actually different. First off, let me point you to this post Part 17: Methods which covers this subject in more depth. But here&#8217;s the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[178],"tags":[],"class_list":["post-5047","post","type-post","status-publish","format-standard","hentry","category-go"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5047"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5047\/revisions"}],"predecessor-version":[{"id":8864,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5047\/revisions\/8864"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}