Pointers in Go: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:
A pointer is a data type that represents a virtual address in memory, usually the address of a location in memory that is referred by a variable.  
A pointer is a data type that represents a virtual address in memory, usually the address of a location in memory that is referred by a variable.  


A pointer can be declared as such:
<syntaxhighlight lang='go'>
var aPtr *int // a pointer to an int
</syntaxhighlight>
It can also be implicitly declared using the [[Go_Language#Short_Variable_Declaration|short variable declaration]] inside functions:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
a := 1
a := 10
aPtr := &a
aPtr := &a
</syntaxhighlight>
</syntaxhighlight>
Line 15: Line 21:
<code>aPtr</code> is a pointer that contains an address that points to the location in memory associated with the variable <code>a</code>. Changing the memory value using a syntax that involves the pointer will surface in the value of the variable:
<code>aPtr</code> is a pointer that contains an address that points to the location in memory associated with the variable <code>a</code>. Changing the memory value using a syntax that involves the pointer will surface in the value of the variable:
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
*aPtr = 2
*aPtr = 20
println(a) // will display 2
println(a) // will display 20
</syntaxhighlight>
</syntaxhighlight>


The pointer data type comes with two operators: <code>&</code> (the [[#The_Referencing_Operator_.26|referencing operator]]), and <code>*</code> (the [[#The_Dereferencing_Operator_.2A|dereferencing operator]]).
The pointer data type comes with two operators: <code>&</code> (the [[#The_Referencing_Operator_.26|referencing operator]]), and <code>*</code> (the [[#The_Dereferencing_Operator_.2A|dereferencing operator]]).


A pointer can be declared as such:
<syntaxhighlight lang='go'>
var ip *int // a pointer to an int
</syntaxhighlight>
=Pointer Variable Name=
=Pointer Variable Name=
<font color=darkkhaki>Do we use <code>someNamePtr</code> or <code>someName</code>?</font>
<font color=darkkhaki>Do we use <code>someNamePtr</code> or <code>someName</code>?</font>

Revision as of 23:44, 28 September 2023

External

Internal

Overview

A pointer is a data type that represents a virtual address in memory, usually the address of a location in memory that is referred by a variable.

A pointer can be declared as such:

var aPtr *int // a pointer to an int

It can also be implicitly declared using the short variable declaration inside functions:

a := 10
aPtr := &a

aPtr is a pointer that contains an address that points to the location in memory associated with the variable a. Changing the memory value using a syntax that involves the pointer will surface in the value of the variable:

*aPtr = 20
println(a) // will display 20

The pointer data type comes with two operators: & (the referencing operator), and * (the dereferencing operator).

Pointer Variable Name

Do we use someNamePtr or someName?

Also see:

Go Language | Variable Names

The Referencing Operator &

The referencing operator (the ampersand operator) returns an address, also known as a "reference", from a variable. & should be read as "address of ...". It works with variables and also with literals. The syntax &user{name:"Bill"} where user is a struct is legal. The address is represented internally as an instance of type pointer. The address points to the location in memory where the instance associated with the "referenced" variable is stored.

&<variable-name>
color := "blue"
pointerToColor := &color
println(pointerToColor) // prints "0xc000058720"

The Dereferencing Operator *

The dereferencing operator (star operator) takes a pointer and returns the value in memory the pointer's address points toward. The variable must contain a pointer type instance, otherwise the code will not compile. The value thus exposed can be read or written.

*<pointer-name>
color := "blue"
pointerToColor := &color
println(*pointerToColor) // prints "blue"
*pointerToColor = "red"
println(color) // prints "red"

When to Use Values and When to Use Pointers