{"id":559,"date":"2017-07-15T20:53:24","date_gmt":"2017-07-16T03:53:24","guid":{"rendered":"http:\/\/www.airs.com\/blog\/?p=559"},"modified":"2017-07-15T20:53:24","modified_gmt":"2017-07-16T03:53:24","slug":"go-experience-report-the-append-function","status":"publish","type":"post","link":"https:\/\/www.airs.com\/blog\/archives\/559","title":{"rendered":"Go experience report: the append function"},"content":{"rendered":"<p>When the Go language was first publicly released it contained a package named container\/vector. \u00a0The type <code>vector.Vector<\/code> was a name for the type <code>[]interface{}<\/code>. \u00a0<code>Vector<\/code> supported methods like <code>Len<\/code>, <code>Cap<\/code>, <code>At<\/code>, <code>Set<\/code>, <code>Insert<\/code>, <code>Delete<\/code>, <code>Push<\/code>, <code>Pop<\/code>, and several more. \u00a0You can see the source code at\u00a0<a href=\"https:\/\/github.com\/golang\/go\/tree\/059c68bf0cccea85bea19c44c15d7fec9e9cbd21\/src\/pkg\/container\/vector\">on Github<\/a>. \u00a0An interesting aspect of the package is that <code>string<\/code> and <code>int<\/code> versions of <code>Vector<\/code> were automatically generated by copying the file vector.go with edits applied by gofmt -r.<\/p>\n<p>The container\/vector package was deleted in October, 2011, before the Go 1 release, because by that time it added nothing that was not available more simply using slices. \u00a0But it was useful once; in October, 2010, it was used by 18 packages in the standard library. \u00a0By the time it was deleted one year later, it was not used by any. \u00a0This change occurred because of a key feature added to the language: the built-in <code>append<\/code> function.<\/p>\n<p>The <code>append<\/code> function was first proposed by Robert Griesemer in an internal e-mail sent in November, 2009, during a discussion of whether to add the built-in <code>copy<\/code> function (as a result of that discussion <code>copy<\/code> was added to the language in <a href=\"https:\/\/golang.org\/cl\/156089\">CL 156089<\/a>). \u00a0At that time I replied about both <code>copy<\/code> and <code>append<\/code> saying &#8220;It seems to me that these functions are not useful if we figure out a way to implement generics. Not necessarily a strong argument against them, I suppose, but do we want to introduce new builtin functions if we may not need them?&#8221;<\/p>\n<p>In 2009 we added <code>copy<\/code> but not <code>append<\/code>. \u00a0In October, 2010, David Symonds proposed <code>append<\/code>\u00a0independently, saying &#8220;It&#8217;s been getting tiresome having to write the same old boilerplate to grow a slice on demand. container\/vector would be suitable if it were generic, but cluttering code with type assertions is worse than the expansion.&#8221; \u00a0Robert picked this up and outlined the function we know today, adding &#8220;Should we ever have some form of genericity, this could even become a library function. For now, the implementation would be a built-in.&#8221; \u00a0I replied to the thread, essentially repeating myself from a year before: &#8220;I&#8217;m not really opposed to a builtin append function but I want to make the obvious comment that, like copy, this is a function we are only considering because we don&#8217;t have generics.&#8221; \u00a0The result of this discussion was to add <code>append<\/code>\u00a0as we know it today to the Go language (<a href=\"https:\/\/golang.org\/cl\/2627043\">CL 2627043<\/a>).<\/p>\n<p>The point I want to make here is that because we had no way to write a generic <code>Vector<\/code> type with an <code>Append<\/code> method, we wound up adding a special purpose language feature to implement it. \u00a0A language that supported parameterized types with methods would not have required a special built-in function that only works with slices. \u00a0An append operation makes sense for other sorts of data structures, such as various kinds of linked lists. \u00a0The built-in <code>append<\/code> function can not be used for them.<\/p>\n<p>I am writing this note as a user experience report to add to the\u00a0<a href=\"https:\/\/golang.org\/wiki\/ExperienceReports\">Go wiki<\/a>. \u00a0To fit the format there, what we wanted to do was to add a generally useful mechanism for extending a slice. \u00a0What we had to do was extend the language. \u00a0That wasn&#8217;t great because it made the language more complex by adding a special purpose, non-generalizable, non-orthogonal, feature.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When the Go language was first publicly released it contained a package named container\/vector. \u00a0The type vector.Vector was a name for the type []interface{}. \u00a0Vector supported methods like Len, Cap, At, Set, Insert, Delete, Push, Pop, and several more. \u00a0You can see the source code at\u00a0on Github. \u00a0An interesting aspect of the package is that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-559","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/comments?post=559"}],"version-history":[{"count":1,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/559\/revisions"}],"predecessor-version":[{"id":560,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/559\/revisions\/560"}],"wp:attachment":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/media?parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/categories?post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/tags?post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}