{"id":283,"date":"2009-12-05T17:16:12","date_gmt":"2009-12-06T01:16:12","guid":{"rendered":"http:\/\/www.airs.com\/blog\/?p=283"},"modified":"2009-12-05T17:16:12","modified_gmt":"2009-12-06T01:16:12","slug":"go-newmake","status":"publish","type":"post","link":"https:\/\/www.airs.com\/blog\/archives\/283","title":{"rendered":"Go New\/Make"},"content":{"rendered":"<p>One of the aspects of Go that some people find confusing is the two predeclared functions, <code>new<\/code> and <code>make<\/code>.  Both functions are specially implemented in the compiler.  They take a type as an argument and return a value.  This can make it seem confusing when you should use one and when you should use the other.  In fact the functions are quite different.<\/p>\n<p>The <code>new<\/code> function is the familiar one from C++ and other languages: it takes a type, allocates a new zero value of that type in the heap, and returns a pointer to the new value.  A call <code>new(T)<\/code> returns a value <code>*T<\/code> (in Go the <code>*<\/code> in a pointer type appears before the type to which it points, not after).  The important point here is that new creates a zero value of the given type.<\/p>\n<p>The <code>make<\/code> function is different.  It creates a non-zero value of the given type.  The <code>make<\/code> function may only be used with a few specific types: slices, maps, and channels.  The zero value for these types is simply <code>nil<\/code>.  In the case of maps and channels, <code>make<\/code> is the only way to create a real, non-<code>nil<\/code>, value.  In the case of a slice, <code>make<\/code> creates an array in the heap (as with <code>new<\/code>) and then returns a pointer to that array converted to a slice.<\/p>\n<p>So you use <code>new<\/code> when you want to allocate space in the heap, e.g., for a linked list.  You use <code>make<\/code> when you want to create a map or channel, or as a convenient shorthand for creating a slice.<\/p>\n<p>Composite literals are also related to <code>new<\/code>, but not <code>make<\/code>.  If you take the address of a composite literal, as in <code>&(struct { i int}){1}<\/code> then you get a new value on the heap.  Each time you execute that code, you get a new one.  So taking the address of a composite literal is similar to using <code>new<\/code>, except that you get a non-zero value.  This serves as a convenient, frequently used, shorthand.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the aspects of Go that some people find confusing is the two predeclared functions, new and make. Both functions are specially implemented in the compiler. They take a type as an argument and return a value. This can make it seem confusing when you should use one and when you should use the [&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-283","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/283","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=283"}],"version-history":[{"count":1,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/283\/revisions"}],"predecessor-version":[{"id":284,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/posts\/283\/revisions\/284"}],"wp:attachment":[{"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/media?parent=283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/categories?post=283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.airs.com\/blog\/wp-json\/wp\/v2\/tags?post=283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}