This commit is contained in:
2026-03-14 19:03:12 +08:00
parent 2805379586
commit 6d86d62825
34 changed files with 3213 additions and 0 deletions
+116
View File
@@ -0,0 +1,116 @@
package main
import "fmt"
func exercise1() func(int) int {
fmt.Println("=== 练习1:基础闭包 ===")
// 创建一个闭包函数,它捕获外部变量multiplier
// 该闭包接收一个int参数并返回乘以multiplier的结果
multiplier := 3
____ := func(num int) ____ {
return ____
}
return ____
}
func exercise2() func() int {
fmt.Println("=== 练习2:闭包状态保持 ===")
// 创建一个递增计数器闭包
// 每次调用返回的函数时,计数器加1
// 提示:使用匿名函数捕获外部变量
count := ____
____ := func() ____ {
____
return ____
}
return ____
}
func exercise3() func() int {
fmt.Println("=== 练习3:斐波那契数列闭包 ===")
// 创建一个生成斐波那契数列的闭包
// 每次调用返回下一个斐波那契数
// 数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
a, b := ____, ____
____ := func() ____ {
result := ____
a, b = ____
return ____
}
return ____
}
func exercise4() func(int) bool {
fmt.Println("=== 练习4:闭包配置 ===")
// 创建一个检查数字是否在范围内的闭包生成器
// 该生成器接受min和max两个参数,返回一个检查函数
// 检查函数判断输入数字是否在[min, max]范围内
min := 10
max := ____
____ := func(num int) ____ {
return num >= ____ && num <= ____
}
return ____
}
func exercise5(start int) func(int) int {
fmt.Println("=== 练习5:闭包累加器 ===")
// 创建一个累加器闭包
// 从start开始,每次调用累加参数值
// 提示:闭包应该能访问外部作用域的变量
total := ____
____ := func(n int) ____ {
____
return ____
}
return ____
}
func main() {
fmt.Println("=== 闭包 练习 ===\n")
add3 := exercise1()
fmt.Printf("练习1结果: add3(4) = %d\n", add3(4))
fmt.Printf("练习1结果: add3(5) = %d\n", add3(5))
counter := exercise2()
fmt.Printf("练习2结果: counter() = %d\n", counter())
fmt.Printf("练习2结果: counter() = %d\n", counter())
fmt.Printf("练习2结果: counter() = %d\n", counter())
fib := exercise3()
fmt.Printf("练习3结果: fib() = %d\n", fib())
fmt.Printf("练习3结果: fib() = %d\n", fib())
fmt.Printf("练习3结果: fib() = %d\n", fib())
fmt.Printf("练习3结果: fib() = %d\n", fib())
fmt.Printf("练习3结果: fib() = %d\n", fib())
inRange := exercise4()
fmt.Printf("练习4结果: inRange(15) = %t\n", inRange(15))
fmt.Printf("练习4结果: inRange(5) = %t\n", inRange(5))
fmt.Printf("练习4结果: inRange(20) = %t\n", inRange(20))
accumulator := exercise5(100)
fmt.Printf("练习5结果: accumulator(10) = %d\n", accumulator(10))
fmt.Printf("练习5结果: accumulator(20) = %d\n", accumulator(20))
fmt.Printf("练习5结果: accumulator(30) = %d\n", accumulator(30))
}
+113
View File
@@ -0,0 +1,113 @@
package main
import (
"bytes"
"os"
"strings"
"testing"
)
func captureOutput(f func()) string {
old := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
f()
w.Close()
os.Stdout = old
var buf bytes.Buffer
buf.ReadFrom(r)
return buf.String()
}
func TestExercise1(t *testing.T) {
add3 := exercise1()
result1 := add3(4)
if result1 != 12 {
t.Errorf("期望 12, 实际: %d", result1)
}
result2 := add3(5)
if result2 != 15 {
t.Errorf("期望 15, 实际: %d", result2)
}
}
func TestExercise2(t *testing.T) {
counter := exercise2()
result1 := counter()
if result1 != 1 {
t.Errorf("期望 1, 实际: %d", result1)
}
result2 := counter()
if result2 != 2 {
t.Errorf("期望 2, 实际: %d", result2)
}
result3 := counter()
if result3 != 3 {
t.Errorf("期望 3, 实际: %d", result3)
}
}
func TestExercise3(t *testing.T) {
fib := exercise3()
expected := []int{0, 1, 1, 2, 3}
for i, exp := range expected {
result := fib()
if result != exp {
t.Errorf("第%d个斐波那契数期望 %d, 实际: %d", i+1, exp, result)
}
}
}
func TestExercise4(t *testing.T) {
inRange := exercise4()
if !inRange(15) {
t.Errorf("15应该在范围内")
}
if inRange(5) {
t.Errorf("5不应该在范围内")
}
if !inRange(20) {
t.Errorf("20应该在范围内")
}
if inRange(21) {
t.Errorf("21不应该在范围内")
}
}
func TestExercise5(t *testing.T) {
accumulator := exercise5(100)
result1 := accumulator(10)
if result1 != 110 {
t.Errorf("期望 110, 实际: %d", result1)
}
result2 := accumulator(20)
if result2 != 130 {
t.Errorf("期望 130, 实际: %d", result2)
}
result3 := accumulator(30)
if result3 != 160 {
t.Errorf("期望 160, 实际: %d", result3)
}
}
func TestMainFunction(t *testing.T) {
output := captureOutput(main)
requiredSections := []string{
"=== 闭包 练习 ===",
"练习1:基础闭包",
"练习2:闭包状态保持",
"练习3:斐波那契数列闭包",
"练习4:闭包配置",
"练习5:闭包累加器",
}
for _, section := range requiredSections {
if !strings.Contains(output, section) {
t.Errorf("期望输出包含 '%s'", section)
}
}
}