初始化

This commit is contained in:
2025-04-08 17:01:19 +08:00
commit 4228f447f7
75 changed files with 574 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
module conditionals
go 1.23.5

View File

@@ -0,0 +1,25 @@
package main
import "fmt"
func main() {
score := 85
// if-else条件语句
if score >= 90 {
fmt.Println("优秀")
} else if score >= 80 {
fmt.Println("良好")
} else if score >= 60 {
fmt.Println("及格")
} else {
fmt.Println("不及格")
}
// 带初始化的if语句
if num := 9; num%2 == 0 {
fmt.Printf("%d是偶数\n", num)
} else {
fmt.Printf("%d是奇数\n", num)
}
}

View File

@@ -0,0 +1,3 @@
module loops
go 1.23.5

View File

@@ -0,0 +1,40 @@
package main
import "fmt"
func main() {
// 基本for循环
fmt.Println("基本for循环:")
for i := 0; i < 5; i++ {
fmt.Printf("%d ", i)
}
fmt.Println()
// while风格的for循环
fmt.Println("\nwhile风格循环:")
n := 0
for n < 3 {
fmt.Printf("%d ", n)
n++
}
fmt.Println()
// 无限循环
fmt.Println("\n无限循环(按条件break):")
count := 0
for {
if count > 3 {
break
}
fmt.Printf("%d ", count)
count++
}
fmt.Println()
// range循环
fmt.Println("\nrange循环:")
nums := []int{10, 20, 30}
for i, num := range nums {
fmt.Printf("索引:%d 值:%d\n", i, num)
}
}