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:
- There is no arithmetic statement that supports prefix increment and decrement, that is, ++a is not allowed.
- 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
- Introduction to the Go language series: a preliminary exploration of the actual combat of the Go project
- Go Programming Journey: Deep Dive into Go Projects
- Go Language Design Philosophy: Understanding Go Why and Design Thinking
- Go Language Advanced Tour: Go deeper into the Go source code
Recommended reading
- Netizen: Go You are from Google, Go: I am not.
- Why does Go have two ways of declaring variables, what's the difference, and which is better?
refer to
- Go FAQ
- Why is the go language grammar designed this way?
- ++ operations for the go language. No auto increment operation?
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。