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 ddf7f29

Browse files
🎨 go-05
1 parent 00bcc8f commit ddf7f29

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

‎go-05/1.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<a href="https://github.com/java-aodeng/golang-examples">第05章 条件和循环</a>
2+
3+
## .序:
4+
开源协议:GPL-3.0
5+
6+
地址:[<a href="https://github.com/java-aodeng/golang-examples">golang-examples</a>] https://github.com/java-aodeng/golang-examples
7+
8+
作者:[<a href="https://github.com/java-aodeng">低调小熊猫</a>] https://github.com/java-aodeng
9+
10+
意义:分享知识,提升自己
11+
12+
## 1.1 循环
13+
与其他主要编程语言的差异
14+
15+
- go语言仅支持循环关键字for
16+
17+
```
18+
func TestWhileLoop(t *testing.T) {
19+
n:=0
20+
for n < 5 {
21+
t.Log(n)
22+
n++
23+
}
24+
}
25+
```
26+
## 1.2 if条件
27+
与其他主要编程语言的差异
28+
29+
- condition表达式结果必须为布尔值
30+
31+
- 支持变量赋值
32+
33+
两段式写法,支持函数写法
34+
```
35+
func TestCondition(t *testing.T) {
36+
if a :=1==1;a {
37+
t.Log(a,"1")
38+
}else {
39+
t.Log(a,"2")
40+
}
41+
}
42+
```
43+
44+
## 1.3 switch条件
45+
与其他主要编程语言的差异
46+
47+
- 条件表达式不限制为常量或者整数
48+
- 单个case中,可以出现多个结果选项,使用逗号分隔
49+
- 与c语言等规则相反,go语言不需要用break来确定退出一个case
50+
- 可以不设定switch之后的条件表达式,在此种情况下,整个switch结构与多个if else 的逻辑作用相同

‎go-05/loop_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package go_05
2+
3+
import "testing"
4+
5+
func TestWhileLoop(t *testing.T) {
6+
n:=0
7+
for n < 5 {
8+
t.Log(n)
9+
n++
10+
}
11+
}
12+
13+
func TestCondition(t *testing.T) {
14+
if a :=1==1;a {
15+
t.Log(a,"1")
16+
}else {
17+
t.Log(a,"2")
18+
}
19+
}
20+
21+
func TestSwitch(t *testing.T) {
22+
23+
}

0 commit comments

Comments
(0)

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