Explore the complete guide to GoLang operators including unary operators (negation, logical NOT, pointer operations), binary operators (arithmetic, bitwise, comparison, logical), and special Go-specific operators like channel send/receive. Master all operator types with detailed examples and real-world use cases.
An operator is special symbol or value that tells the programming language to perform on one or more number of values (which are called as operands) and produce a result. To give you a better and simpler version to understand, in a single English language sentence verb is operator and the nouns are the operands.
Based on the number of operands involved in the statement, the operators are classified into three types.
Unary Operator
Binary Operator
Special Operator (specific to Go Lang, other languages have a ternary operator)
Unary Operators
This classification is based on single operator, the official documentation will have some differences.
Unary operator
Name
Works on
What it does
Quick example
Common “stuck” moment
+x
Unary plus
Numeric types
Returns x (defined as 0 + x)
+n
Often used just for symmetry; doesn’t change the value.
-x
Negation
Numeric types
Negates x (defined as 0 - x)
-balance
Works on numeric types (int/float/complex), not on strings.
!p
Logical NOT
bool
Flips truth value (“not p”)
!ok
Only valid on booleans (not ints like in C).
^x
Bitwise complement
Integers
Inverts bits of x (bitwise NOT)
^mask
People confuse with XOR: binary XOR is a ^ b, unary is ^x.
&x
Address-of
Addressable values (or composite literals)
Produces a pointer *T pointing to x
p := &v
& needs addressable operand (variable/field/index), except composite literals like &Point{1,2}.
*p
Pointer indirection (dereference)
Pointers *T
Accesses the value pointed to by p
v := *p
Panics if p is nil.
<-ch
Receive
Channels
Receives a value from ch (blocks until available)
v := <-ch
Receiving from nil blocks forever; receiving from closed channel yields zero value (after buffer drains).
Binary Operator
Category
Operator
Name
Works on
What it does
Quick example
Common “stuck” moment
Arithmetic
+
sum / concat
numbers, strings
Adds numbers; concatenates strings
a+b, "go"+"lang"
+ works on strings too (creates a new string).
Arithmetic
-
difference
numbers
Subtracts right from left
a-b
Only numeric (not strings).
Arithmetic
*
product
numbers
Multiplies
a*b
Watch overflow with ints.
Arithmetic
/
quotient
numbers
Divides
a/b
With integers it truncates; divisor 0 panics.
Arithmetic
%
remainder
integers
Remainder after truncated division
a%b
Only integers; divisor 0 panics; remainder can be negative.
Bitwise
&
AND
integers
Bitwise AND
flags & mask
Useful for checking bits/flags.
Bitwise
|
OR
integers
Bitwise OR
flags | mask
Used for setting bits.
Bitwise
^
XOR
integers
Bitwise XOR
a ^ b
Don’t confuse with unary ^x (bitwise complement).
Bitwise
&^
bit clear (AND NOT)
integers
Clears bits present in right operand
flags &^ mask
Think: “remove these bits from flags”.
Shift
<<
left shift
integers
Shifts left by count (count must be >= 0)
x << 2
Can overflow for signed ints; count negative at runtime panics.
Shift
>>
right shift
integers
Shifts right by count (count must be >= 0)
x >> 1
Signed vs unsigned matters: arithmetic vs logical shift.
Comparison
==
equal
comparable types
True if operands are equal
a == b
Slices/maps/functions aren’t comparable (except to nil).
Comparison
!=
not equal
comparable types
True if operands are not equal
a != b
Same comparability rule as ==.
Comparison
<
less
ordered types
True if left is less
a < b
Only ordered types (numbers, strings, etc.).
Comparison
<=
less or equal
ordered types
True if left is less or equal
a <= b
Make sure types are assignable to each other.
Comparison
>
greater
ordered types
True if left is greater
a > b
Same “ordered types only” rule.
Comparison
>=
greater or equal
ordered types
True if left is greater or equal
a >= b
Same “ordered types only” rule.
Logical
&&
conditional AND
booleans
If p then q else false (short-circuit)
p && q
q runs only if p is true.
Logical
||
conditional OR
booleans
If p then true else q (short-circuit)
p || q
q runs only if p is false.
Special Operators
Operator / Syntax
What it does
Example
Notes / gotchas
<-ch
Receives a value from a channel (and returns it).
v := <-ch
Blocks until a value is available; receiving from a nil channel blocks forever. Can do v, ok := <-ch to detect closed+empty channel. (Go)
ch <- v
Sends a value on a channel.
ch <- 3
Blocks until send can proceed (buffer/receiver). Send on closed channel panics. Send on nil channel blocks forever. (Go)
:=
Short variable declaration (declare + initialize).
i, j := 0, 10
Only inside functions. May redeclare existing vars in same block if at least one new var is introduced. (Go)
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
[…] Operators in Go Lang […]