Go Built-In Function new: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Go Concepts")
 
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
* [[Go_Functions#new|Go Functions]]


* [[Go Concepts#Built-in_Functions|Go Concepts]]
=Overview=
<code>new()</code> can be used to create [[Go_Structs#new|new struct instances]].
 
=TO DEPLETE=
<font color=darkkhaki>
 
==Internal==
 
* [[Go Concepts - Functions#Built-in_Functions|Built-in Functions]]
* [[Go Concepts - Lexical Structure#Pre-Declared_Identifiers|Pre-declared Identifiers]]
 
==Overview==
 
The <tt>new()</tt> built-in function takes a type as argument, allocates memory of the given type and returns ''a pointer'' to it.
 
<pre>
var ptr *some_type
ptr = new(some_type)
</pre>
 
We don't have to worry about de-allocating memory allocated with <tt>new()</tt>. It is automatically [[Go_Concepts_-_Memory_Model#Garbage_Collection|garbage collected]].
 
==<tt>new()</tt> and <tt>New()</tt>==
 
The "[[Go Language Error Handling#The_.22errors.22_Package|errors]]" package exposes a <tt>New()</tt> function. Do not mistake it for <tt>new()</tt>.

Latest revision as of 22:34, 30 August 2023

Internal

Overview

new() can be used to create new struct instances.

TO DEPLETE

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().