feat(auth): 添加完整的用户认证API项目
- 实现用户注册、登录、JWT令牌认证功能 - 集成Gin、GORM、Viper、Zap等框架 - 添加密码加密、数据库操作、中间件等完整功能 - 配置多环境支持、日志轮转、CORS处理 - 创建完整的项目结构和配置文件体系
This commit is contained in:
42
go并发模型/04go-context-practice/derived_context.go
Normal file
42
go并发模型/04go-context-practice/derived_context.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func subTask(ctx context.Context, name string, d time.Duration) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
fmt.Println(name, "提前被取消:", ctx.Err())
|
||||
case <-time.After(d):
|
||||
fmt.Println(name, "完成,用时", d)
|
||||
}
|
||||
}
|
||||
|
||||
func mainTask(ctx context.Context) {
|
||||
// 从上游 ctx 派生两个子 context
|
||||
ctx1, cancel1 := context.WithCancel(ctx)
|
||||
defer cancel1()
|
||||
|
||||
ctx2, cancel2 := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel2()
|
||||
|
||||
go subTask(ctx1, "subTask-1", 5*time.Second)
|
||||
go subTask(ctx2, "subTask-2", 5*time.Second)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Println("mainTask: 主动取消 subTask-1 的 ctx1")
|
||||
cancel1()
|
||||
|
||||
// 等待一会儿,看 subTask-2 是否因超时被取消
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
|
||||
func main() {
|
||||
root := context.Background()
|
||||
fmt.Println("开始 mainTask...")
|
||||
mainTask(root)
|
||||
fmt.Println("main 结束")
|
||||
}
|
||||
Reference in New Issue
Block a user