Operands
September 11, 2023
I’ll be live coding again today! I hope you can join me! I’ll be continuing where I left off, working on a new feature for my open-source CouchDB SDK, https://kivik.io/. Join me to see how many mistakes a senior Go dev makes while coding.
To kick off our dissection of expressions, we’ll look at the term “operand”. You may recall from your studies of algebra that an operand is the “object upon which an operator acts.” The Go spec explains it this way:
Operands
Operands denote the elementary values in an expression.
Key here is the word “values”. Operands are values. Not operators. Not function calls. Not data types. Not colors or flavors of ice cream. They’re values.
The spec goes on:
An operand may be a literal, a (possibly qualified) non-blank identifier denoting a constant, variable, or function, or a parenthesized expression.
Operand = Literal | OperandName [ TypeArgs ] | "(" Expression ")" . Literal = BasicLit | CompositeLit | FunctionLit . BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit . OperandName = identifier | QualifiedIdent .
So let’s look at some simple examples of operands used in simple arethmetic operations. (Much more complicated operations and operands are possible, but we’ll save those for later discussion.)
const x = 1 + 2 // `1` and `2` are literal operands
y := x + 3 // `x` is a named operand
z := math.MaxInt - y // `math.MaxInt` is a qualified, named constant as an operand
Quotes from The Go Programming Language Specification Version of August 2, 2023