Expressions, Statements And Simple Statements

This article will introduce expressions and statements in Go.

Simply speaking, an expressions represents a value and a statement represents an operation. However, in fact, some special expressions each may be composed of and represent several values, and some statements may be composed of several sub operations/statements. By context, some statements can be also viewed as expressions.

Simple statements are some special statements. In Go, some portions of all kinds of control flows must be simple statements, and some portions must be expressions. Control flows will be introduced in the next Go 101 article.

This article will not make accurate definitions for expressions and statements. It is hard to achieve this. This article will only list some expression and statement cases. Not all kinds of expressions and statements will be covered in this article, but all kinds of simple statements will be listed.

Some Expression Cases

Most expressions in Go are single-value expressions. Each of them represents one value. Some expressions each represents multiple values. Such expressions are called multi-value expressions.

Later, out of the currrent article, unless otherwise specified, when an expression is mentioned, we mean it is a single-value expression.

Value literals, variables and named constants are all single-value expressions. They also called elementary expressions.

The operations (without the assignment parts) using the operators introduced in the article common operators are all single-value expressions.

If a function returns at least one result, then its calls (without the assignment parts) are expressions. In particular, if the function returns more than one results, the its calls belong to multi-value expressions. Calls to functions without results are not expressions.

Methods can be viewed as special functions. So the above mentioned function cases also apply to methods. Methods will be explained in detail in the article method in Go later.

In fact, later we will learn that custom functions, including methods, are all function values, so they are also (single-value) expressions. We can learn more about function types and values later.

Channel receive operations (without the assignment parts) are also expressions. Channel operations will be explained in the article channels in Go later.

Some expressions in Go, including the just mentioned channel receive operations, may have optional results in Go. Such expressions can present as both signle-value and multi-value expressions, depending on different contextes. We can learn such expressions in other Go 101 articles later.

Simple Statement Cases

There are six kinds of simple statements.
  1. short variable declaration forms
  2. pure value assignments (not mixing with variable declarations), including x op= y operations.
  3. function/method calls and channel receive operations. As mentioned in the last section, these simple statements can also be used as expressions.
  4. channel send operations.
  5. nothing (a.k.a., blank statements). We will learn some uses of blank statements in the next article.
  6. x++ and x--.

Again, channel receive and sent operations will be incroduced in the article channels in Go.

Note, x++ and x-- can't be used as expressions. And Go doesn't support the ++x and --x syntax forms.

Some Non-Simple Statement Cases

An incomplete non-simple statements list:

Examples Of Expressions And Statements

// Some non-simple statements.
import "time"
var a = 123
const B = "Go"
type Choice bool
func f() int {
	for a < 10 {
		break
	}

	// This is an explicit code block.
	{
		// ...
	}
	return 567
}

// Some simple statements.
c := make(chan bool) // c is a channel value of type "chan bool".
a = 789
a += 5
f()     // here f() is a simple statement.
a = f() // here f() is used as the source value of a pure assignment.
a++
a--
c <- true // a channel send operation (a simple statement).
<-c       // a channel receive operation (used as a simple statement).
z := <-c  // a channel receive operation (used as an expression).

// Some expressions.
123
true
B
B + " language"
a - 789
a > 0 // an untyped boolean value
f()   // an expression which can be viewed as an int value.
f     // a function value of type "func ()".
<-c   // an expression which can be viewed as a bool value.
      // It can also be used as a simple statement.


The Go 101 project is hosted on both github and gitlab. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

Support Go 101 by playing Tapir's games. Cryptocurrency donations are also welcome:
Bitcoin: 1xucQbv5jujFPPwhyg395ri5yV71hx9g9
Ethereum: 0x5dc4aa2c2bbfaadae373dadcfca11b3358912212