Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41f3e38

Browse files
RickyWang1020Tinywan
authored andcommitted
Update golang_tutorial_08.md
1 parent 9550fbb commit 41f3e38

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

‎docs/golang_tutorial_08.md‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func main() {
7272
}
7373
```
7474

75-
在上面的程序中, `num``if `语句中初始化。需要注意的一点是,`num` 只能在 if 和 else 里面进行访问,即 `num` 的范围仅限于`if else`块中。如果我们试图在 `if``else` 之外访问 `num`,编译器将报错。
75+
在上面的程序中, `num``if `语句中初始化。需要注意的一点是,`num` 只能在 if 和 else 里面进行访问,即 `num` 的范围仅限于`if else`块中。如果我们试图在 `if``else` 之外访问 `num`,编译器将报错。
7676

7777
让我们用 else if 再写一个程序:
7878

@@ -95,13 +95,13 @@ func main() {
9595
}
9696
```
9797

98-
在上面的程序 `else if num >= 51 && num <= 100``true`,因此程序将输出:`number is between 51 and 100`
98+
在上面的程序 `else if num >= 51 && num <= 100``true`,因此程序将输出:`number is between 51 and 100`
9999

100-
## 疑难杂症
100+
## 小陷阱
101101

102-
`else`声明应该在`if`语句的大括号`}`之后的同一行中开始。如果不是,编译器会报错
102+
`else` 语句应该在 `if`语句的大括号`}`之后的同一行中开始,否则编译器会报错
103103

104-
让我们通过一个程序来理解这一点。
104+
让我们通过一个程序来理解这一点。
105105

106106
```golang
107107
package main
@@ -121,17 +121,17 @@ func main() {
121121
}
122122
```
123123

124-
在上面的程序中,`else`语句不会在if 语句`}`关闭后的同一行中开始。相反,它从下一行开始。Go中不允许这样做。如果你运行这个程序,编译器会输出错误:
124+
在上面的程序中,`else` 语句没有在 `if` 语句 `}` 结束后的同一行中开始,而是从下一行开始的。Go中不允许这样做。如果你运行这个程序,编译器会输出错误:
125125

126126
```golang
127127
main.go:12:5: syntax error: unexpected else, expecting }
128128
```
129129

130-
***原因是Go自动插入分号的方式。你可以在这里阅读https://golang.org/ref/spec#Semicolons 的分号插入规则。***
130+
***这是由于Go会自动插入分号,点击链接了解分号插入规则:https://golang.org/ref/spec#Semicolons***
131131

132-
在规则中,指定在`}`后面插入分号,如果这是分号的最后一个标记。所以在if语句`}`之后自动插入一个分号
132+
在分号插入规则中,如果 `}` 是当前一行的最后一个字符,那么 `}` 后面会被插入一个分号。所以在上面的例子中,if语句的 `}` 之后会被自动插入一个分号
133133

134-
所以我们的程序变成了:
134+
所以实际上我们的程序变成了:
135135

136136
```golang
137137
if num%2 == 0 {
@@ -142,11 +142,11 @@ else {
142142
}
143143
```
144144

145-
分号插入后。您可以在行号中看到分号插入。上面的代码段中的3个。
145+
你可以看到第三行中插入了一个分号。
146146

147-
既然`if{...} else {...}`是一个单一的陈述,分号就不应该出现在它的中间。因此,需要`else`在关闭后放置在同一行中`}`
147+
由于 `if{...} else {...}` 是一个单独的语句,分号不应该出现在语句的中间。因此,`else` 需要放在 `}` 之后的同一行中
148148

149-
我通过`}`在if语句关闭后移动`else`来重写程序,以防止自动分号插入。
149+
我重新改写了程序,将 `else` 移动到if语句结束之后的 `}` 的后面,以防止自动分号插入。
150150

151151
```golang
152152
package main
@@ -165,6 +165,6 @@ func main() {
165165
}
166166
```
167167

168-
现在编译器会很开心,我们也是如此。
168+
现在编译器就可以正常编译我们的程序了。
169169

170-
希望你喜欢阅读。请留下宝贵的意见和反馈:)
170+
希望你喜欢阅读。请留下宝贵的意见和反馈:)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /