Primary expressions

October 17, 2023

Today we just have the definition of a term. We’ll be building on this term in the following days.

Primary expressions

Primary expressions are the operands for unary and binary expressions.

PrimaryExpr =
	Operand |
	Conversion |
	MethodExpr |
	PrimaryExpr Selector |
	PrimaryExpr Index |
	PrimaryExpr Slice |
	PrimaryExpr TypeAssertion |
	PrimaryExpr Arguments .

Selector       = "." identifier .
Index          = "[" Expression [ "," ] "]" .
Slice          = "[" [ Expression ] ":" [ Expression ] "]" |
                 "[" [ Expression ] ":" Expression ":" Expression "]" .
TypeAssertion  = "." "(" Type ")" .
Arguments      = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" .

Recall from before that “operands denote the elementary values in an expression”. And here we’re concerned with the operands for unary and binary expressions.

What are those?

A unary expression is an expression that stands on its own. Something like a variable name, x. Or maybe -x, to apply the negation unary operator - to x.

And a binary expression doesn’t mean two expressions, though. It means an expression composed of binary operators, such as x || y.

So a primary expression is an expression that can be used as either of these. The spec goes on to show some examples:

x
2
(s + ".txt")
f(3.1415, true)
Point{1, 2}
m["foo"]
s[i : j + 1]
obj.color
f.p[i].x()

Quotes from The Go Programming Language Specification Version of August 2, 2023


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Expression statements

No livestream today. I wasn’t feeling well enough. No live stream next week either, as I’ll be traveling. Expression statements With the exception of specific built-in functions, function and method calls and receive operations can appear in statement context. Such statements may be parenthesized. ExpressionStmt = Expression . The following built-in functions are not permitted in statement context: append cap complex imag len make new real unsafe.Add unsafe.Alignof unsafe.Offsetof unsafe.Sizeof unsafe.


Statements

At long last, we have completed the section of the spec on expressions… and now we move on to: Statements Statements control execution. Statement = Declaration | LabeledStmt | SimpleStmt | GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt | FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt | DeferStmt . SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl . In my experience, the terms “expression” and “statement” and “declaration” are often confused and conflated.


Operator precedence

I hope we all know what operator precedence means… but just in case it’s fuzzy, I’ll illustrate with a simple example from junior high school. What does this mean? 1 + 2 * 3 It’s either 9 or 7, right? It depends on the order in which we apply the + and * operations. I’m sure most of us agree that the correct answer is actually 7, because multiplication takes precedence over addition.

Get daily content like this in your inbox!

Subscribe