현재 3권의 신간들인 Go Optimizations 101, Go Details & Tips 101Go Generics 101이 출간되어 있습니다. Leanpub 서점에서 번들을 모두 구입하시는 방법이 비용 대비 효율이 가장 좋습니다.

Go에 대한 많은 정보들과 Go 101 책들의 최신 소식을 얻으시려면 Go 101 트위터 계정인 @Go100and1을 팔로잉 해주세요.

Expressions, Statements and Simple Statements

This article will introduce expressions and statements in Go.

Simply speaking, an expression represents a value and a statement represents an operation. However, in fact, some special expressions 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. Other expressions represent multiple values and they are named multi-value expressions.

In the scope of this document, when an expression is mentioned, we mean it is a single-value expression, unless otherwise specified.

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

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 a function returns more than one results, then its calls belong to multi-value expressions. Calls to functions without results are not expressions.

Methods can be viewed as special functions. So the aforementioned 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 will 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 channel receive operations, may have optional results in Go. Such expressions can present as both single-value and multi-value expressions, depending on different contexts. 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 introduced 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) // channels will be explained later
a = 789
a += 5
a = f() // here f() is used as an expression
a++
a--
c <- true // a channel send operation
z := <-c  // a channel receive operation used as the
          // source value in an assignment statement.

// Some expressions:
123
true
B
B + " language"
a - 789
a > 0 // an untyped boolean value
f     // a function value of type "func ()"

// The following ones can be used as both
// simple statements and expressions.
f()
<-c // a channel receive operation


Index↡

The Go 101 프로젝트는 Github 에서 호스팅됩니다. 오타, 문법 오류, 부정확한 표현, 설명 결함, 코드 버그, 끊어진 링크와 같은 모든 종류의 실수에 대한 수정 사항을 제출하여 Go 101을 개선을 돕는 것은 언제나 환영합니다.

주기적으로 Go에 대한 깊이 있는 정보를 얻고 싶다면 Go 101의 공식 트위터 계정인 @go100and1을 팔로우하거나 Go 101 슬랙 채널에j가입해주세요.

이 책의 디지털 버전은 아래와 같은 곳을 통해서 구매할 수 있습니다.
Go 101의 저자인 Tapir는 2016년 7월부터 Go 101 시리즈 책들을 집필하고 go101.org 웹사이트를 유지 관리하고 있습니다. 새로운 콘텐츠는 책과 웹사이트에 수시로 추가될 예정입니다. Tapir는 인디 게임 개발자이기도 합니다. Tapir의 게임을 플레이하여 Go 101을 지원할 수도 있습니다. (안드로이드와 아이폰/아이패드용):
  • Color Infection (★★★★★), 140개 이상의 단계로 이루어진 물리 기반의 캐주얼 퍼즐 게임
  • Rectangle Pushers (★★★★★), 2가지 모드와 104개 이상의 단계로 이루어진 캐주얼 퍼즐 게임
  • Let's Play With Particles, 세가지 미니 게임이 있는 캐주얼 액션 게임
페이팔을 통한 개인 기부도 환영합니다.

색인: