Skip to content

Commit 063f9e9

Browse files
author
maxcook
committed
update gobase file
1 parent ee15fd7 commit 063f9e9

File tree

12 files changed

+471
-153
lines changed

12 files changed

+471
-153
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/go-web-api-study.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/api/main.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func serveSourceCode(filePath string) http.HandlerFunc {
1616
http.Error(w, "文件不存在: "+filePath, http.StatusNotFound)
1717
return
1818
}
19-
19+
2020
w.Header().Set("Content-Type", "text/html; charset=utf-8")
2121
fmt.Fprintf(w, `
2222
<html>
@@ -61,7 +61,7 @@ func main() {
6161
http.NotFound(w, r)
6262
return
6363
}
64-
64+
6565
w.Header().Set("Content-Type", "text/html; charset=utf-8")
6666
fmt.Fprintf(w, `
6767
<html>
@@ -182,39 +182,44 @@ func main() {
182182
<a href="/gobase/01">查看源代码</a>
183183
</div>
184184
<div class="module-card">
185-
<h3>02 - 函数</h3>
186-
<p>函数定义、参数、返回值、闭包、defer</p>
185+
<h3>02 - 切片与映射 CRUD</h3>
186+
<p>切片/映射的创建、读取、更新、删除及清空技巧</p>
187187
<a href="/gobase/02">查看源代码</a>
188188
</div>
189189
<div class="module-card">
190-
<h3>03 - 结构体和接口</h3>
191-
<p>结构体、方法、接口、类型断言</p>
190+
<h3>03 - 函数</h3>
191+
<p>函数定义、参数、返回值、闭包、defer</p>
192192
<a href="/gobase/03">查看源代码</a>
193193
</div>
194194
<div class="module-card">
195-
<h3>04 - 并发编程</h3>
196-
<p>goroutine、channel、select、同步</p>
195+
<h3>04 - 结构体和接口</h3>
196+
<p>结构体、方法、接口、类型断言</p>
197197
<a href="/gobase/04">查看源代码</a>
198198
</div>
199+
<div class="module-card">
200+
<h3>05 - 并发编程</h3>
201+
<p>goroutine、channel、select、同步</p>
202+
<a href="/gobase/05">查看源代码</a>
203+
</div>
199204
<div class="module-card http">
200-
<h3>05 - HTTP基础</h3>
205+
<h3>06 - HTTP基础</h3>
201206
<p>HTTP服务器、处理器、中间件、客户端</p>
202-
<a href="/gobase/05">查看源代码</a>
207+
<a href="/gobase/06">查看源代码</a>
203208
</div>
204209
<div class="module-card http">
205-
<h3>06 - API开发</h3>
210+
<h3>07 - API开发</h3>
206211
<p>RESTful API、CRUD操作、JSON处理、错误处理</p>
207-
<a href="/gobase/06">查看源代码</a>
212+
<a href="/gobase/07">查看源代码</a>
208213
</div>
209214
<div class="module-card database">
210-
<h3>07 - 数据库操作</h3>
215+
<h3>08 - 数据库操作</h3>
211216
<p>SQL操作、连接池、事务、ORM基础</p>
212-
<a href="/gobase/07">查看源代码</a>
217+
<a href="/gobase/08">查看源代码</a>
213218
</div>
214219
<div class="module-card advanced">
215-
<h3>08 - 高级特性</h3>
220+
<h3>09 - 高级特性</h3>
216221
<p>并发模式、反射、泛型、性能优化、设计模式</p>
217-
<a href="/gobase/08">查看源代码</a>
222+
<a href="/gobase/09">查看源代码</a>
218223
</div>
219224
</div>
220225
</body>
@@ -228,19 +233,20 @@ func main() {
228233

229234
// Go基础模块源代码查看
230235
http.HandleFunc("/gobase/01", serveSourceCode("gobase/01_variables_and_types.go"))
231-
http.HandleFunc("/gobase/02", serveSourceCode("gobase/02_functions.go"))
232-
http.HandleFunc("/gobase/03", serveSourceCode("gobase/03_structs_and_interfaces.go"))
233-
http.HandleFunc("/gobase/04", serveSourceCode("gobase/04_concurrency.go"))
234-
http.HandleFunc("/gobase/05", serveSourceCode("gobase/05_http_basics.go"))
235-
http.HandleFunc("/gobase/06", serveSourceCode("gobase/06_api_development.go"))
236-
http.HandleFunc("/gobase/07", serveSourceCode("gobase/07_database_basics.go"))
237-
http.HandleFunc("/gobase/08", serveSourceCode("gobase/08_advanced_features.go"))
236+
http.HandleFunc("/gobase/02", serveSourceCode("gobase/02_slices_maps.go"))
237+
http.HandleFunc("/gobase/03", serveSourceCode("gobase/02_functions.go"))
238+
http.HandleFunc("/gobase/04", serveSourceCode("gobase/03_structs_and_interfaces.go"))
239+
http.HandleFunc("/gobase/05", serveSourceCode("gobase/04_concurrency.go"))
240+
http.HandleFunc("/gobase/06", serveSourceCode("gobase/05_http_basics.go"))
241+
http.HandleFunc("/gobase/07", serveSourceCode("gobase/06_api_development.go"))
242+
http.HandleFunc("/gobase/08", serveSourceCode("gobase/07_database_basics.go"))
243+
http.HandleFunc("/gobase/09", serveSourceCode("gobase/08_advanced_features.go"))
238244

239245
fmt.Println("🚀 Go Web API 学习服务器启动成功!")
240246
fmt.Println("📱 访问地址: http://localhost:8080")
241247
fmt.Println("📚 练习目录: http://localhost:8080/exercises")
242248
fmt.Println("🔧 基础模块: http://localhost:8080/gobase")
243249
fmt.Println("💚 健康检查: http://localhost:8080/health")
244-
250+
245251
log.Fatal(http.ListenAndServe(":8080", nil))
246-
}
252+
}

exercises/README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,33 @@ exercises/
1515

1616
## 🎯 使用方法
1717

18-
### 1. 每日练习
18+
### 1. 学习顺序
19+
1. 先阅读并运行 `gobase/` 中的对应示例,理解知识点与 CRUD 操作
20+
2. 再对照示例,在 `exercises/dayXX/` 自行完成练习文件(可照抄、可改写、可扩展)
21+
3. 运行练习文件,观察输出,确保与示例一致
22+
23+
### 2. 每日练习
1924
每天在对应的 `dayXX/` 目录下完成练习:
20-
- 创建 `.go` 文件
21-
- 编写练习代码
22-
- 运行测试
25+
- 创建 `.go` 文件(或复制示例后修改)
26+
- 编写练习代码,务必亲手实现 CRUD 各步
27+
- 运行测试,对比 gobase 输出
2328

24-
### 2. 命令行运行
29+
### 3. 命令行运行
2530
```bash
26-
# 运行第1天的练习
27-
go run exercises/day01/hello_world.go
31+
# 1. 先运行 gobase 示例,看懂输出
32+
go run gobase/01_variables_and_types.go # 变量/结构体 CRUD
33+
go run gobase/02_slices_maps.go # 切片与映射 CRUD
2834

29-
# 运行第2天的练习
35+
# 2. 再运行自己的练习
36+
go run exercises/day01/hello_world.go
3037
go run exercises/day02/variables_practice.go
38+
go run exercises/day03/variables_practice.go # 切片练习
39+
go run exercises/day04/variables_practice.go # 映射练习
40+
```
41+
42+
#### 运行第4天的练习(Map CRUD)
43+
```
44+
go run exercises/day04/variables_practice.go
3145
```
3246

3347
### 3. Web端查看
@@ -46,9 +60,9 @@ go run exercises/day02/variables_practice.go
4660

4761
### 第一周:Go语言基础入门
4862
- **Day 01**: Hello World + 基础语法
49-
- **Day 02**: 变量声明和基本类型
50-
- **Day 03**: 变量进阶:类型转换和常量
51-
- **Day 04**: 函数定义和基本调用
63+
- **Day 02**: 变量与结构体 CRUD(创建/读取/更新/置空)
64+
- **Day 03**: 切片 CRUD(创建/读取/更新/删除/清空)
65+
- **Day 04**: Map CRUD(创建/读取/更新/删除/遍历/清空)
5266
- **Day 05**: 函数进阶:参数、返回值、闭包
5367
- **Day 06**: 结构体定义和基本使用
5468
- **Day 07**: 结构体进阶:方法和嵌套
@@ -103,4 +117,10 @@ go run exercises/day02/variables_practice.go
103117
- 每个练习文件都应该是可独立运行的
104118
- 在文件开头添加日期和学习内容注释
105119
- 遇到问题时,可以参考 `gobase/` 目录下的示例代码
106-
- 完成练习后,可以在Web界面查看和分享你的代码
120+
- 完成练习后,可以在Web界面查看和分享你的代码
121+
122+
## ✅ 已覆盖的核心操作
123+
124+
- 变量与结构体:创建、读取、更新、置空(零值与指针 nil)
125+
- 切片:创建、追加、读取、更新、删除(单元素与区间)、清空与置为 nil
126+
- Map:创建、读取(含 ok 习语)、更新、删除、遍历、清空(重建与逐个删除)
Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,86 @@
1-
// Day 02 - 变量和类型练习
1+
// Day 02 - 变量与结构体 CRUD 练习
22
// 日期: 2025年x月 (请根据实际日期修改)
3-
// 学习内容: 变量声明、基本类型、类型转换
3+
// 学习内容: 变量与结构体的 创建(增)、读取(查)、更新(改)、置空(“删”)
44

55
package main
66

77
import "fmt"
88

9+
// Person 用于结构体 CRUD 演示
10+
type Person struct {
11+
Name string
12+
Age int
13+
Job string
14+
}
15+
16+
func variableCRUD() {
17+
fmt.Println("\n[变量 CRUD]")
18+
19+
// 增: 创建变量(声明 + 赋值)
20+
var name string = "Go 学习者"
21+
var age int = 25
22+
isStudent := false // 短变量声明
23+
fmt.Printf("创建 -> 姓名:%s 年龄:%d 学生:%v\n", name, age, isStudent)
24+
25+
// 查: 读取变量
26+
fmt.Printf("读取 -> name=%q age=%d isStudent=%v\n", name, age, isStudent)
27+
28+
// 改: 更新变量(重新赋值)
29+
name = "Gopher"
30+
age += 1
31+
isStudent = true
32+
fmt.Printf("更新 -> 姓名:%s 年龄:%d 学生:%v\n", name, age, isStudent)
33+
34+
// “删”: 将变量置为零值/空值(Go 没有真正的删除变量语义)
35+
name = "" // 字符串零值
36+
age = 0 // 数值零值
37+
isStudent = false
38+
fmt.Printf("置空 -> 姓名:%q 年龄:%d 学生:%v\n", name, age, isStudent)
39+
}
40+
41+
func structCRUD() {
42+
fmt.Println("\n[结构体 CRUD]")
43+
44+
// 增: 创建结构体实例
45+
p := Person{Name: "Alice", Age: 30, Job: "Engineer"}
46+
fmt.Printf("创建 -> %+v\n", p)
47+
48+
// 查: 读取字段
49+
fmt.Printf("读取 -> Name=%s Age=%d Job=%s\n", p.Name, p.Age, p.Job)
50+
51+
// 改: 更新字段
52+
p.Age++
53+
p.Job = "Senior Engineer"
54+
fmt.Printf("更新 -> %+v\n", p)
55+
56+
// “删”: 将结构体置为零值(或指针置为 nil)
57+
p = Person{} // 所有字段置为零值
58+
fmt.Printf("置空 -> %+v\n", p)
59+
60+
// 指针形式的“删”演示
61+
pp := &Person{Name: "Bob", Age: 28, Job: "Designer"}
62+
fmt.Printf("指针创建 -> %+v\n", *pp)
63+
pp = nil
64+
fmt.Printf("指针置空 -> %v\n", pp)
65+
}
66+
67+
func numberTypes() {
68+
fmt.Println("\n[数值类型与转换]")
69+
var i8 int8 = 127
70+
var f32 float32 = 3.1415927
71+
var f64 float64 = 3.141592653589793
72+
fmt.Printf("int8=%d f32=%.7f f64=%.15f\n", i8, f32, f64)
73+
74+
// 类型转换演示
75+
i32 := int32(i8)
76+
f := float64(i32)
77+
fmt.Printf("转换 -> int32=%d float64=%.0f\n", i32, f)
78+
}
79+
980
func main() {
10-
fmt.Println("=== Day 02: 变量和类型练习 ===")
11-
12-
// TODO: 在这里完成今天的练习
13-
// 1. 声明不同类型的变量
14-
var name string = "go 语言爱好者"
15-
var age int = 25
16-
var isStudnet bool = false
17-
fmt.Printf("姓名:%s, 年龄:%d, 是否学生:%v\n", name, age, isStudnet)
18-
19-
//短变量声明(只能在函数内使用)
20-
day := 2
21-
year := 2025
22-
version := "v1.0.0"
23-
fmt.Printf("今天是第 %d 天练习,%d年, 版本号为:%s\n", day, year, version)
24-
25-
//基本数据类型
26-
var int8Val int8 = 127
27-
28-
// 浮点类型
29-
var float32Val float32 = 3.141592653589793
30-
var float64Val float64 = 3.141592653589793
31-
32-
fmt.Printf("整数类型: %d\n", int8Val)
33-
fmt.Printf("float32浮点类型: %f\n", float32Val)
34-
fmt.Printf("float64浮点类型: %f\n", float64Val)
35-
36-
// 2. 练习类型转换
37-
// 3. 使用常量
38-
// 4. 数组和切片操作
39-
40-
fmt.Println("今天的练习: 变量和类型")
41-
fmt.Println("请完成上面的TODO项目")
81+
fmt.Println("=== Day 02: 变量与结构体 CRUD 练习 ===")
82+
83+
variableCRUD()
84+
structCRUD()
85+
numberTypes()
4286
}

0 commit comments

Comments
 (0)