Go Concepts - Operators: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
=TO DEPLETE=
<font color=darkkhaki>
=External=
=External=


* Go Specification - Operators: https://golang.org/ref/spec#Operators
* Go Specification - Operators: https://golang.org/ref/spec#Operators
=Internal=
* [[Go Concepts - Lexical Structure#Operators|Go Lexical Structure]]


=+=
=+=
Line 16: Line 16:


Subtraction
Subtraction
=*=
<tt>*</tt> is the multiplication operator.
As "address operator", <tt>*</tt> is used with pointers as dereference operator. For more details see [[Go_Concepts_-_Lexical_Structure#Reference_and_Dereference_Operators|reference and dereference operators]].
<tt>*</tt> designates [[Go_Concepts_-_The_Type_System#Pointer_Types|pointer types]].


=<tt>&</tt>=
=<tt>&</tt>=
Line 50: Line 42:


Can be used to test [[Go_Strings#String_Equality|string equality]].
Can be used to test [[Go_Strings#String_Equality|string equality]].
=[]=
"<tt>[]</tt>" is the ''indexing operator''. If the index is out of bounds, the runtime generates a run-time panic:
<pre>
panic: runtime error: index out of range
</pre>
Applies to:
* [[Go Strings#Indexing_Operator|strings]]
* [[Go_Arrays#Indexing_Operator|arrays]]
* [[Go_Slices#Indexing_Operator|slices]]
* [[Go_Maps#Indexing_Operator|maps]]


= := =
= := =
Line 75: Line 53:
=<tt>.</tt>=
=<tt>.</tt>=


Used for:
<tt>.</tt> is used for ''field access'' and ''method calls'':
* access of a <tt>struct</tt>'s fields.
 
* ''method call'': invoke a method on the ''instance'' of the type the [[Go_Concepts_-_Functions#Method_Invocation|method]] is associated with, or on the ''pointer'' to an instance of the type the method is associated with.
==Field Access==
 
Used to access of a <tt>struct</tt>'s fields.


<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
:When invoking a method, the ... operator can be used both with a value or a pointer. In both situations it has the same semantics.
:When used for field access, <tt>.</tt> can be used both with a struct value or a pointer to that struct value. In both situations it has the same semantics.
</blockquote>
</blockquote>
Also see [[Go_Structs#Fields|fields]]
==Method Call==
Used to invoke a method on the ''instance'' of the type the [[Go_Concepts_-_Functions#Method_Invocation|method]] is associated with, or on the ''pointer'' to an instance of the type the method is associated with.
<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
:When designating a method call, <tt>.</tt> can be used both with a value or a pointer. In both situations it has the same semantics. If <tt>ptr</tt> is a pointer, then <tt>ptr.m()</tt> is a shorthand for <tt>(&ptr).m()</tt>,
</blockquote>
Also see [[Go_Concepts_-_Functions#Methods|methods]].
==Function Call==
Used to invoke a function from a package:
<pre>
fmt.Println(...)
</pre>
</font>

Latest revision as of 18:21, 24 August 2023

TO DEPLETE

External

+

Addition or concatenation. The compiler figures out the semantics based on the operands' types.

Applies to:

-

Subtraction

&

& is the reference operator. For more details see reference and dereference operators.

/

Division

%

Remainder

=

The assignment operator.

+=

Addition and assignment.

==

The equality operator. Returns a boolean value.

Can be used to test string equality.

:=

Short variable declaration operator. It does variable declaration and assignment (initialization). The type of the variable is inferred from the right side expression.

<-

<- is the left arrow operator. It is used to send and receive messages on channels.

.

. is used for field access and method calls:

Field Access

Used to access of a struct's fields.

When used for field access, . can be used both with a struct value or a pointer to that struct value. In both situations it has the same semantics.

Also see fields

Method Call

Used to invoke a method on the instance of the type the method is associated with, or on the pointer to an instance of the type the method is associated with.

When designating a method call, . can be used both with a value or a pointer. In both situations it has the same semantics. If ptr is a pointer, then ptr.m() is a shorthand for (&ptr).m(),

Also see methods.

Function Call

Used to invoke a function from a package:

fmt.Println(...)