Expressions, Statements And Simple Statements

This article will introduce expressions and statements in Go.

Simply speaking, expressions represent values and statements represent operations. 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. And some expressions can also be used as statements.

Simple statements are some special statements. In Go, some portions in 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 express 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

Value literals, named constants and variables are all expressions. They also called elementary values.

The operations (without the assignment part) using the operators introduced in the article common operators are all expressions. There are more operator operations which will be introduced in other articles are also expressions.

If a function returns exact one result, then its calls (without the assignment parts) are expressions. Calls of functions without results are not expressions. We have learned that a function call may return multiple results in the article function declarations and calls. Such function calls are called multi-valued expressions. Multi-value expressions can only be be used at some specific senarios.

Most expressions in Go a single-valued expression. Later, out of the currrent article, unless otherwise specified, when an expression is mentioned, we mean it is a single-valued expression.

Methods can be viewed as special functions. So the above mentioned function cases also apply to methods. Methods will explained detailedly 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 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 may have optional results in Go. 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 without the assignment parts. Some of these simple statements can also be used as expressions.
  4. channel send operations.
  5. nothing. This is called blank statement.
  6. x++ and x--.

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

Yes, Go supports the two syntaxes. However, the two forms can't be used as values. In other words, they are not expressions. x++ is equivalent to x = x + 1, and x-- is equivalent to x = x - 1, where expression x will only be evaludated once. Go doesn't support the ++x and --x syntaxes.

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 {

	// This is an solo explicit code block.
		// ...
	return 567

// Some simple Statements.
c := make(chan bool) // c is a 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.
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 is used as an expression.

// Some expressions.
B + " language"
a - 789
a > 0 // an untyped boolean value
f()   // an expression which can be viewed as an int value.
f     // an function value of type "func ()".
<-c   // an expression which can be viewed as a bool value.
Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies description flaws and code bugs.