2

Hello everyone, I am fried fish.

Habitual comparison and learning by analogy are habitual associated operations when everyone masters new skills. A more flavorful programming language like Go has a detail that everyone is curious about.

In fact, Go only supports post-increment/auto-decrement. Today, Fried Fish and everyone will study why.

grammar

Basic Go increment, very simple. See the code directly:

 a := 1
a++

fmt.Println(a)

Output result:

 2

If the answer output is wrong, it is recommended to turn the grammar right. Next, take a look at a few other examples to see if the results are consistent with your expected results.

Example 1, the code is as follows:

 func main() {
    a := 1
    b := a++
    fmt.Println(b)
}

The result of the output:

 # command-line-arguments
./main.go:9:8: syntax error: unexpected ++ at end of statement

Example 2, the code is as follows:

 func main() {
    a := 1
    ++a
    fmt.Println(a)
}

The result of the output:

 # command-line-arguments
./main.go:9:2: syntax error: unexpected ++, expecting }

You'll find both examples, normal in other common languages. But in Go it will run wrong?

reason

Go by design:

  1. There is no arithmetic statement that supports prefix increment and decrement, that is, ++a is not allowed.
  2. The operators ++ and -- can only be used as a statement, and cannot be used as expressions to assign to other variables.

Refer to the following example:

  • In a statement, ++ is OK.
  • ++ is not allowed in assignment=.

So why is it not supported? In essence, the designers of Go are to make the code more readable, and do not need to worry about the order of evaluation.

From the point of view of the program, whether the prefix is incremented or the suffix is incremented, the operation results are the same. However, once it is introduced, it will increase the possibility of programmers making mistakes, and there will often be confusion. Occasionally, some people will make interview questions to test candidates.

Obviously, prefixes and assignments are not supported, and ++ and -- can only be used as a statement to improve the readability of Go code, and the simplification is of great significance.

Summarize

In today's article, we explored and discussed the details of ++ and -- in Go syntax design. In fact, a++, or --a, or a more complex mixed expression, can only confuse later friends during interviews or writing.

It does not bring too much benefit on the road of Go engineering, so it is naturally removed.

Have you ever tried to get confused by all kinds of weird prefixes, suffixes, mixes?

The article is continuously updated, you can read it on WeChat by searching [Brain Fried Fish]. This article has been included in GitHub github.com/eddycjy/blog . To learn Go language, you can see the Go learning map and route . Welcome to Star to urge you to update.

Go Book Series

Recommended reading

refer to

  • Go FAQ
  • Why is the go language grammar designed this way?
  • ++ operations for the go language. No auto increment operation?

煎鱼
8.4k 声望12.8k 粉丝