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 .