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 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.
Value literals, named constants and variables are all expressions. They also called elementary values.
The operations (without the assignment parts) 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 be 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.
x op= y
operations.
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.
{
and ends with a }
.
A code block may contain many sub statements.
return
lines in function declarations.
// Some non-simple statements.
import "time"
var a = 123
const B = "Go"
type Choice bool
func f() int {
for a < 10 {
break
}
// This is a solo 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.