Compare commits

...
9 Commits
Author SHA1 Message Date
wonder 3744760c4e Finish: 03-maps - map使用 2026-03-19 11:07:28 +08:00
wonder a6cba26de2 Finish: 02-slices - 切片操作 2026-03-19 10:47:43 +08:00
wonder 6ccc22ec9f Finish: 01-arrays - 数组 2026-03-19 10:31:59 +08:00
wonder 95b45fb1b6 Finish: 04-higher-order - 高阶函数 2026-03-19 10:20:24 +08:00
wonder 0e6a3b24f3 Finish: 03-closures - 闭包 2026-03-17 16:06:21 +08:00
wonder 9b6e6badaa Finish: 02-multiple-returns - 多返回值 2026-03-17 15:53:13 +08:00
wonder d570ed6810 Finish: 01-basic-functions - 基础函数 2026-03-17 15:46:46 +08:00
wonder 787dd02bb3 Finish: 04-defer - defer机制 2026-03-17 15:37:09 +08:00
wonder e6db364f3e Finish: 03-for-loop - for循环 2026-03-17 15:24:34 +08:00
9 changed files with 157 additions and 155 deletions
+11 -13
View File
@@ -2,7 +2,7 @@ package main
import "fmt"
func exercise1() int {
func exercise1() {
fmt.Println("=== 练习1:基本for循环 ===")
sum := 0
@@ -11,15 +11,14 @@ func exercise1() int {
// 初始化: i := 1
// 条件: i <= 10
// 后置: i++
for ____; ____; ____ {
for i := 1; i <= 10; i++ {
sum += i
}
fmt.Printf("1到10的和: %d\n", sum)
return sum
}
func exercise2() int {
func exercise2() {
fmt.Println("\n=== 练习2:while形式的for循环 ===")
sum := 0
@@ -27,13 +26,12 @@ func exercise2() int {
// 使用while形式的for循环,计算1到5的乘积
// 提示:省略初始化和后置语句
for ____ {
for i <= 5 {
sum *= i
i++
}
fmt.Printf("1到5的乘积: %d\n", sum)
return sum
}
func exercise3() []int {
@@ -46,12 +44,12 @@ func exercise3() []int {
// 如果数字大于8,停止循环(使用break)
i := 1
for i <= 10 {
if ____ {
if i%3 == 0 {
i++
____
continue
}
if ____ {
____
if i > 8 {
break
}
nums = append(nums, i)
i++
@@ -69,7 +67,7 @@ func exercise4() string {
// 使用for-range遍历切片,格式化输出
// 提示:range会返回索引和值,示例: "0: 苹果"
for ____, ____ := range ____ {
for index, value := range fruits {
result += fmt.Sprintf("%d: %s ", index, value)
}
@@ -85,7 +83,7 @@ func exercise5() string {
// 使用for-range遍历map,格式化输出
// 提示:range遍历map时,返回的是键和值
for ____, ____ := range ____ {
for name, score := range scores {
result += fmt.Sprintf("%s: %d ", name, score)
}
@@ -94,7 +92,7 @@ func exercise5() string {
}
func main() {
fmt.Println("=== for循环 练习 ===\n")
fmt.Println("=== for循环 练习 ===")
exercise1()
exercise2()
+12 -12
View File
@@ -6,7 +6,7 @@ func exercise1() {
fmt.Println("=== 练习1:defer基本用法 ===")
// 使用defer,在函数结束时打印"函数结束"
____
defer fmt.Println("函数结束")
fmt.Println("函数执行中")
}
@@ -15,9 +15,9 @@ func exercise2() {
// 使用多个defer语句,按照1-2-3的顺序声明
// 观察输出的顺序(应该是3-2-1)
____
____
____
defer fmt.Print(1)
defer fmt.Print(2)
defer fmt.Print(3)
fmt.Println("正常执行")
}
@@ -27,13 +27,13 @@ func exercise3() int {
// 使用具名返回值result
// defer函数中将result修改为100
// return返回50
var ____ int = ____
var result int = 100
defer func() {
____ = ____
result = 50
}()
return ____
return result
}
func exercise4() {
@@ -43,7 +43,7 @@ func exercise4() {
// 使用defer打印i的值
// 注意:defer声明时就对参数求值了
____
defer fmt.Println(i)
i = 10
fmt.Println("修改i为:", i)
@@ -58,7 +58,7 @@ func exercise5() {
// 使用defer在函数结束时关闭资源
defer func() {
fmt.Println("关闭:", ____)
fmt.Println("关闭:", resource)
}()
fmt.Println("使用:", resource)
@@ -71,15 +71,15 @@ func exercise6() {
// 每个defer捕获当前i的值并打印
// 提示:使用闭包或立即求值
for i := 1; i <= 3; i++ {
____ func(n int) {
defer func(n int) {
fmt.Printf("defer输出: %d\n", n)
}(____)
}(i)
fmt.Println("循环输出:", i)
}
}
func main() {
fmt.Println("=== defer 练习 ===\n")
fmt.Println("=== defer 练习 ===")
exercise1()
exercise2()
+22 -18
View File
@@ -1,6 +1,9 @@
package main
import "fmt"
import (
"fmt"
"strings"
)
func exercise1() int {
fmt.Println("=== 练习1:无参数单返回值函数 ===")
@@ -9,14 +12,11 @@ func exercise1() int {
// 定义一个函数cube,接收一个int参数,返回其立方
// 调用这两个函数并返回它们的和
____ func ____
square := func(n int) ____ { return n * n }
____
}
square := func(n int) int { return n * n }
cube := func(n int) ____ { ____ }
cube := func(n int) int { return n * n * n }
return ____(3) + ____(2)
return square(3) + cube(2)
}
func exercise2(a, b int) {
@@ -25,19 +25,23 @@ func exercise2(a, b int) {
// 定义一个函数,接收两个int参数a和b
// 打印a + b, a - b, a * b
// 格式: "加: 5, 减: 1, 乘: 6"
fmt.Printf("加: %d, 减: %d, 乘: %d\n", ____, ____, ____)
sum := a + b
difference := a - b
product := a * b
fmt.Printf("加: %d, 减: %d, 乘: %d\n", sum, difference, product)
}
func exercise3(nums ...int) int {
fmt.Println("\n=== 练习3:可变参数 ===")
// 定义一个可变参数函数,计算所有数字的和
sum := ____
for _, num := range ____ {
____
sum := 0
for _, num := range nums {
sum += num
}
return ____
return sum
}
func exercise4(name, city string) (string, string) {
@@ -47,13 +51,13 @@ func exercise4(name, city string) (string, string) {
// nameWithGreeting: 格式为 "Hello, {name}"
// info: 格式为 "{name} lives in {city}"
var ____ string
var ____
var nameWithGreeting string
var info string
nameWithGreeting = fmt.Sprintf("Hello, %s", name)
info = fmt.Sprintf("%s lives in %s", name, city)
return ____, ____
return nameWithGreeting, info
}
func exercise5(name string) string {
@@ -63,13 +67,13 @@ func exercise5(name string) string {
// 返回大写的name
// 提示:使用strings.ToUpper
____ := ____
upperName := strings.ToUpper(name)
return ____
return upperName
}
func main() {
fmt.Println("=== 基础函数 练习 ===\n")
fmt.Println("=== 基础函数 练习 ===")
result1 := exercise1()
fmt.Printf("练习1结果: %d\n", result1)
+11 -13
View File
@@ -1,42 +1,40 @@
package main
import (
"errors"
"fmt"
"time"
)
func exercise1(x, y int) (int, int) {
// 返回x+y和x-y
return ____, ____
return x + y, x - y
}
func exercise2(n int) (int, error) {
// 如果n<0,返回0和错误
// 否则返回n和nil
if ____ {
return ____, ____
if n < 0 {
return n, fmt.Errorf("division by zero")
}
return ____, ____
return n, nil
}
func exercise3(s string) (bool, int) {
// 返回字符串是否包含"hello"和字符串长度
contains := true
length := ____
return ____, ____
length := len(s)
return contains, length
}
func main() {
fmt.Println("=== 多返回值 练习 ===\n")
fmt.Println("=== 多返回值 练习 ===")
sum, diff := exercise1(10, 3)
fmt.Printf("练习1: 加=%d, 减=%d\n", ____, ____)
fmt.Printf("练习1: 加=%d, 减=%d\n", sum, diff)
result, err := exercise2(-5)
if ____ {
fmt.Printf("练习2: 错误 = %v\n", ____)
if err != nil {
fmt.Printf("练习2: 错误 = %v\n", result)
} else {
fmt.Printf("练习2: 结果 = %d\n", ____)
fmt.Printf("练习2: 结果 = %d\n", result)
}
}
+24 -24
View File
@@ -9,11 +9,11 @@ func exercise1() func(int) int {
// 该闭包接收一个int参数并返回乘以multiplier的结果
multiplier := 3
____ := func(num int) ____ {
return ____
multifunc := func(num int) int {
return multiplier * num
}
return ____
return multifunc
}
func exercise2() func() int {
@@ -23,14 +23,14 @@ func exercise2() func() int {
// 每次调用返回的函数时,计数器加1
// 提示:使用匿名函数捕获外部变量
count := ____
count := 0
____ := func() ____ {
____
return ____
countfunc := func() int {
count++
return count
}
return ____
return countfunc
}
func exercise3() func() int {
@@ -40,15 +40,15 @@ func exercise3() func() int {
// 每次调用返回下一个斐波那契数
// 数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
a, b := ____, ____
a, b := 0, 1
____ := func() ____ {
result := ____
a, b = ____
return ____
fib := func() int {
result := a
a, b = b, a+b
return result
}
return ____
return fib
}
func exercise4() func(int) bool {
@@ -59,13 +59,13 @@ func exercise4() func(int) bool {
// 检查函数判断输入数字是否在[min, max]范围内
min := 10
max := ____
max := 20
____ := func(num int) ____ {
return num >= ____ && num <= ____
check := func(num int) bool {
return num >= min && num <= max
}
return ____
return check
}
func exercise5(start int) func(int) int {
@@ -75,18 +75,18 @@ func exercise5(start int) func(int) int {
// 从start开始,每次调用累加参数值
// 提示:闭包应该能访问外部作用域的变量
total := ____
total := start
____ := func(n int) ____ {
____
return ____
adder := func(n int) int {
total += n
return total
}
return ____
return adder
}
func main() {
fmt.Println("=== 闭包 练习 ===\n")
fmt.Println("=== 闭包 练习 ===")
add3 := exercise1()
fmt.Printf("练习1结果: add3(4) = %d\n", add3(4))
+23 -21
View File
@@ -8,9 +8,9 @@ func exercise1(apply func(int) int, value int) int {
// 接收一个函数apply和一个值value
// 调用apply函数处理value并返回结果
result := ____
result := apply(value)
return ____
return result
}
func exercise2() func(int) int {
@@ -19,7 +19,9 @@ func exercise2() func(int) int {
// 返回一个函数,该函数将输入值乘以2
// 提示:返回一个匿名函数
return ____
return func(x int) int {
return x * 2
}
}
func exercise3(vals []int, filter func(int) bool) []int {
@@ -28,15 +30,15 @@ func exercise3(vals []int, filter func(int) bool) []int {
// 接收一个int切片和一个过滤函数
// 返回过滤后的切片(只保留满足条件的元素)
var result ____
var result []int
for _, val := range ____ {
if ____(val) {
result = append(result, ____)
for _, val := range vals {
if filter(val) {
result = append(result, val)
}
}
return ____
return result
}
func exercise4(vals []int, mapper func(int) int) []int {
@@ -45,13 +47,13 @@ func exercise4(vals []int, mapper func(int) int) []int {
// 接收一个int切片和一个映射函数
// 返回映射后的切片(每个元素都经过mapper函数处理)
result := make([]int, ____)
result := make([]int, len(vals))
for i, val := range ____ {
result[i] = ____(val)
for i, val := range vals {
result[i] = mapper(val)
}
return ____
return result
}
func exercise5(vals []int, reducer func(int, int) int, initial int) int {
@@ -61,13 +63,13 @@ func exercise5(vals []int, reducer func(int, int) int, initial int) int {
// 返回归约结果
// 例如:对切片求和、求积等
result := ____
result := initial
for _, val := range ____ {
result = ____(result, val)
for _, val := range vals {
result = reducer(result, val)
}
return ____
return result
}
func exercise6() int {
@@ -79,18 +81,18 @@ func exercise6() int {
// 返回 double(square(3)) 的结果
// 即:先平方(9),再双倍(18)
square := func(n int) int { return ____ }
double := func(n int) int { return ____ }
square := func(n int) int { return n * n }
double := func(n int) int { return n * 2 }
compose := func(f, g func(int) int, x int) int {
return ____(____(x))
return g(f(x))
}
return compose(____, ____, 3)
return compose(square, double, 3)
}
func main() {
fmt.Println("=== 高阶函数 练习 ===\n")
fmt.Println("=== 高阶函数 练习 ===")
平方 := func(n int) int { return n * n }
result1 := exercise1(平方, 4)
+17 -17
View File
@@ -7,13 +7,13 @@ func exercise1() [5]int {
// 声明一个长度为5的int数组
// 初始化所有元素为10
var arr ____
var arr [5]int
for i := 0; i < ____(arr); i++ {
arr[i] = ____
for i := 0; i < len(arr); i++ {
arr[i] = 10
}
return ____
return arr
}
func exercise2() string {
@@ -22,7 +22,7 @@ func exercise2() string {
// 修改数组元素并返回修改后的字符串表示
fruits := [4]string{"苹果", "香蕉", "橙子", "葡萄"}
fruits[____] = "芒果"
fruits[0] = "芒果"
fmt.Printf("修改后的水果: %s\n", fruits)
@@ -35,13 +35,13 @@ func exercise3() int {
// 计算数组所有元素的和
numbers := [5]int{10, 20, 30, 40, 50}
sum := ____
sum := 0
for i := 0; i < ____(numbers); i++ {
sum += ____
for i := 0; i < len(numbers); i++ {
sum += numbers[i]
}
return ____
return sum
}
func exercise4() [3]int {
@@ -51,11 +51,11 @@ func exercise4() [3]int {
// 修改原数组,观察拷贝的数组是否变化
arr1 := [3]int{1, 2, 3}
arr2 := ____
arr2 := arr1
arr1[0] = 99
return ____
return arr2
}
func exercise5() int {
@@ -64,19 +64,19 @@ func exercise5() int {
// 计算二维数组的和
matrix := [3][2]int{{1, 2}, {3, 4}, {5, 6}}
sum := ____
sum := 0
for i := 0; i < ____(matrix); i++ {
for j := 0; j < ____(matrix[i]); j++ {
sum += ____
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[i]); j++ {
sum += matrix[i][j]
}
}
return ____
return sum
}
func main() {
fmt.Println("=== 数组 练习 ===\n")
fmt.Println("=== 数组 练习 ===")
result1 := exercise1()
fmt.Printf("练习1结果: %v\n", result1)
+19 -19
View File
@@ -6,10 +6,10 @@ func exercise1() []int {
fmt.Println("=== 练习1:切片声明与初始化 ===")
// 创建一个长度为3的int切片,初始值为[1, 2, 3]
s := make([]int, ____)
s[0], s[1], s[2] = ____, ____, ____
s := make([]int, 3)
s[0], s[1], s[2] = 1, 2, 3
return ____
return s
}
func exercise2() int {
@@ -17,10 +17,10 @@ func exercise2() int {
// 向切片中添加元素并返回新长度
fruits := []string{"苹果", "香蕉"}
fruits = append(fruits, ____)
fruits = append(fruits, ____, ____)
fruits = append(fruits, "梨子")
fruits = append(fruits, "蓝莓", "草莓")
return ____(fruits)
return len(fruits)
}
func exercise3() []int {
@@ -29,14 +29,14 @@ func exercise3() []int {
// 从切片中提取子切片
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// 提取索引2到6(不包含6)
sub1 := numbers[____:____]
// 提取索引2到5(不包含5)
sub1 := numbers[2:5]
// 提取前5个元素
sub2 := numbers[:____]
sub2 := numbers[:5]
// 提取从第5个开始到末尾
sub3 := numbers[____:]
sub3 := numbers[4:]
fmt.Printf("子切片1: %v\n", sub1)
fmt.Printf("子切片2: %v\n", sub2)
@@ -50,13 +50,13 @@ func exercise4() int {
// 修改原始切片,观察使用copy复制的切片是否受影响
s1 := []int{1, 2, 3}
s2 := make([]int, ____(s1))
s2 := make([]int, len(s1))
copy(____, s1)
copy(s2, s1)
s1[0] = 99
return ____
return s2[0]
}
func exercise5() []int {
@@ -65,19 +65,19 @@ func exercise5() []int {
// 创建一个新切片,包含原切片中所有偶数
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var evens ____
var evens []int
for _, num := range ____ {
if num%2 == ____ {
evens = append(____, num)
for _, num := range numbers {
if num%2 == 0 {
evens = append(evens, num)
}
}
return ____
return evens
}
func main() {
fmt.Println("=== 切片 练习 ===\n")
fmt.Println("=== 切片 练习 ===")
result1 := exercise1()
fmt.Printf("练习1结果: %v\n", result1)
+18 -18
View File
@@ -7,13 +7,13 @@ func exercise1() map[string]int {
// 创建一个map,键为string类型,值为int类型
// 添加"张三": 90, "李四": 85, "王五": 95
scores := ____
scores := make(map[string]int)
scores["张三"] = ____
scores["李四"] = ____
scores["王五"] = ____
scores["张三"] = 90
scores["李四"] = 85
scores["王五"] = 95
return ____
return scores
}
func exercise2() string {
@@ -25,7 +25,7 @@ func exercise2() string {
"李四": 85,
}
score, exists := ____
score, exists := scores["李四"]
if exists {
fmt.Printf("李四的分数是: %d\n", score)
@@ -45,13 +45,13 @@ func exercise3() int {
"赵六": 88,
}
sum := ____
sum := 0
for ____, score := range ____ {
sum += ____
for _, score := range scores {
sum += score
}
return ____
return sum
}
func exercise4() map[string]int {
@@ -64,9 +64,9 @@ func exercise4() map[string]int {
"王五": 95,
}
____(scores, "李四")
delete(scores, "李四")
return ____
return scores
}
func exercise5() []string {
@@ -81,19 +81,19 @@ func exercise5() []string {
"钱七": 92,
}
var topStudents ____
var topStudents []string
for name, score := range ____ {
if score >= ____ {
topStudents = append(____, ____)
for name, score := range scores {
if score >= 90 {
topStudents = append(topStudents, name)
}
}
return ____
return topStudents
}
func main() {
fmt.Println("=== map 练习 ===\n")
fmt.Println("=== map 练习 ===")
result1 := exercise1()
fmt.Printf("练习1结果: %v\n", result1)