Go Built-In Function new: Difference between revisions
Jump to navigation
Jump to search
Line 21: | Line 21: | ||
=<tt>new()</tt> and <tt>New()</tt>= | =<tt>new()</tt> and <tt>New()</tt>= | ||
The "errors" package exposes a <tt>New()</tt> function. Do not mistake it for <tt>new()</tt>. | The "[[Go_Concepts_-_Error_Handling#The_.22errors.22_Package|errors]]" package exposes a <tt>New()</tt> function. Do not mistake it for <tt>new()</tt>. |
Revision as of 04:14, 4 April 2016
External
- Specification https://golang.org/ref/spec#Allocation
Internal
Overview
The new() built-in function takes a type as argument, allocates memory of the given type and returns a pointer to it.
var ptr *some_type ptr = new(some_type)
We don't have to worry about de-allocating memory allocated with new(). It is automatically garbage collected.
new() and New()
The "errors" package exposes a New() function. Do not mistake it for new().