Operators in Go Lang

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)
Hand-drawn sketchnote with a banner “Understanding Operators.” It defines operators as symbols that perform operations on operands and produce a result. A simple English sentence diagram shows “X eats Y,” labeling X and Y as operands (nouns) and “eats” as the operator (verb). Below, a “Types of Operators” section branches into three boxes: “Unary Operator” with example “-X” and note “one operand” (examples: -X, !A, &P), “Binary Operator” with examples like “A + B” and “P == Q” and note “two operands,” and “Special Operator (Go Lang)” illustrating channel send and receive using arrows with ch <- val and <- ch, labeled as Go-specific channel operations.

Unary Operators

This classification is based on single operator, the official documentation will have some differences.

Unary operatorNameWorks onWhat it doesQuick exampleCommon “stuck” moment
+xUnary plusNumeric typesReturns x (defined as 0 + x)+nOften used just for symmetry; doesn’t change the value.
-xNegationNumeric typesNegates x (defined as 0 - x)-balanceWorks on numeric types (int/float/complex), not on strings.
!pLogical NOTboolFlips truth value (“not p”)!okOnly valid on booleans (not ints like in C).
^xBitwise complementIntegersInverts bits of x (bitwise NOT)^maskPeople confuse with XOR: binary XOR is a ^ b, unary is ^x.
&xAddress-ofAddressable values (or composite literals)Produces a pointer *T pointing to xp := &v& needs addressable operand (variable/field/index), except composite literals like &Point{1,2}.
*pPointer indirection (dereference)Pointers *TAccesses the value pointed to by pv := *pPanics if p is nil.
<-chReceiveChannelsReceives a value from ch (blocks until available)v := <-chReceiving from nil blocks forever; receiving from closed channel yields zero value (after buffer drains).

Binary Operator

CategoryOperatorNameWorks onWhat it doesQuick exampleCommon “stuck” moment
Arithmetic+sum / concatnumbers, stringsAdds numbers; concatenates stringsa+b, "go"+"lang"+ works on strings too (creates a new string).
Arithmetic-differencenumbersSubtracts right from lefta-bOnly numeric (not strings).
Arithmetic*productnumbersMultipliesa*bWatch overflow with ints.
Arithmetic/quotientnumbersDividesa/bWith integers it truncates; divisor 0 panics.
Arithmetic%remainderintegersRemainder after truncated divisiona%bOnly integers; divisor 0 panics; remainder can be negative.
Bitwise&ANDintegersBitwise ANDflags & maskUseful for checking bits/flags.
Bitwise|ORintegersBitwise ORflags | maskUsed for setting bits.
Bitwise^XORintegersBitwise XORa ^ bDon’t confuse with unary ^x (bitwise complement).
Bitwise&^bit clear (AND NOT)integersClears bits present in right operandflags &^ maskThink: “remove these bits from flags”.
Shift<<left shiftintegersShifts left by count (count must be >= 0)x << 2Can overflow for signed ints; count negative at runtime panics.
Shift>>right shiftintegersShifts right by count (count must be >= 0)x >> 1Signed vs unsigned matters: arithmetic vs logical shift.
Comparison==equalcomparable typesTrue if operands are equala == bSlices/maps/functions aren’t comparable (except to nil).
Comparison!=not equalcomparable typesTrue if operands are not equala != bSame comparability rule as ==.
Comparison<lessordered typesTrue if left is lessa < bOnly ordered types (numbers, strings, etc.).
Comparison<=less or equalordered typesTrue if left is less or equala <= bMake sure types are assignable to each other.
Comparison>greaterordered typesTrue if left is greatera > bSame “ordered types only” rule.
Comparison>=greater or equalordered typesTrue if left is greater or equala >= bSame “ordered types only” rule.
Logical&&conditional ANDbooleansIf p then q else false (short-circuit)p && qq runs only if p is true.
Logical||conditional ORbooleansIf p then true else q (short-circuit)p || qq runs only if p is false.

Special Operators

Operator / SyntaxWhat it doesExampleNotes / gotchas
<-chReceives a value from a channel (and returns it).v := <-chBlocks until a value is available; receiving from a nil channel blocks forever. Can do v, ok := <-ch to detect closed+empty channel. (Go)
ch <- vSends a value on a channel.ch <- 3Blocks 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, 10Only inside functions. May redeclare existing vars in same block if at least one new var is introduced. (Go)

srnyapathi
srnyapathi
Articles: 41

One comment

Comments are closed.