{"id":5192,"date":"2017-06-26T22:01:33","date_gmt":"2017-06-26T22:01:33","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5192"},"modified":"2017-06-26T22:01:33","modified_gmt":"2017-06-26T22:01:33","slug":"anonymous-fields-in-go","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/anonymous-fields-in-go\/","title":{"rendered":"Anonymous fields in Go"},"content":{"rendered":"<p>Anonymous fields in Go structures allow us to shortcut dot notation as well as allow us to use compositions to add methods to a type.<\/p>\n<p><strong>Shortcutting the dot notation<\/strong><\/p>\n<p>For example if we have something like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntype A struct {\r\n\tX int\r\n\tY int\r\n}\r\n\r\ntype B struct {\r\n\tZ int\r\n}\r\n\r\ntype C struct {\r\n\tAValue A\r\n\tBValue B\r\n}\r\n<\/pre>\n<p>then to access the field X on AValue we&#8217;d write code like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nc := C {}\r\nc.AValue.X = 1\r\n<\/pre>\n<p>Changing the C struct fields to anonymous (i.e. removing the field name) like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntype C struct {\r\n\tA\r\n\tB\r\n}\r\n<\/pre>\n<p>allows us to reduce the use of the dot notation and access X on A as if it was a field  within the C struct itself, i.e.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nc := C {}\r\nc.X = 1\r\n<\/pre>\n<p>Okay, but what if B struct now replaced the Z field name with X, i.e. <\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntype A struct {\r\n\tX int\r\n\tY int\r\n}\r\n\r\ntype B struct {\r\n\tX int\r\n}\r\n<\/pre>\n<p>We now have a situation where both A and B have the same field name, well we can still use anonymous fields, but we&#8217;re back to using more dot notation again to get around the obvious field name conflict, i.e.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nc := C {}\r\nc.A.X = 1\r\nc.B.X = 2\r\n<\/pre>\n<p><strong>Initializers do not support shortcutting<\/strong><\/p>\n<p>Unfortunately we not get the advantage of the anonymous fields with the initializer syntax, i.e. We must specify the A and B structs like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nc := C{ A : A{2, 4}, B : B { 12 }}\r\n\r\n\/\/ or\r\n\r\nc := C{ A : A{X : 2, Y : 4}, B : B { Z: 12 }}\r\n\r\n\/\/ but this doesn't compile\r\nc := C{ X : 2, Y : 4, Z : 12 }\r\n\/\/ nor does this compile\r\nc := C{ A.X : 2, A.Y = 3, B.Z : 12 }\r\n<\/pre>\n<p><strong>Composition<\/strong><\/p>\n<p>More powerful than the synaptic sugar of embedded\/anonymous fields, we can also use composition to increase the methods available on the struct C. For example let&#8217;s now add a method to the A type which allows us to move our X, Y point<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nfunc (a *A) Move(amount int) {\r\n\ta.X += amount\r\n\ta.Y += amount\r\n}\r\n<\/pre>\n<p>Now we can call the method on the previously declared <em>c<\/em> variable like this<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nc.Move(10)\r\n<\/pre>\n<p>We can also create further methods via a composition pattern on type C if we want using empty types, let&#8217;s assume we have type D and extend the composition of type C<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\ntype D struct {}\r\n\r\ntype C struct {\r\n\tA\r\n\tB\r\n\tD\r\n}\r\n<\/pre>\n<p>now we can add methods to D and ofcourse they&#8217;ll be available as part of C. So for example, maybe D acts as a receiver for methods that serialize data to JSON, now our C type will also appear to have those methods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Anonymous fields in Go structures allow us to shortcut dot notation as well as allow us to use compositions to add methods to a type. Shortcutting the dot notation For example if we have something like this type A struct { X int Y int } type B struct { Z int } type C [&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-5192","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\/5192","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=5192"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5192\/revisions"}],"predecessor-version":[{"id":5197,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5192\/revisions\/5197"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}