feat(auth): 添加完整的用户认证API项目

- 实现用户注册、登录、JWT令牌认证功能
- 集成Gin、GORM、Viper、Zap等框架
- 添加密码加密、数据库操作、中间件等完整功能
- 配置多环境支持、日志轮转、CORS处理
- 创建完整的项目结构和配置文件体系
This commit is contained in:
liumangmang
2025-12-30 18:00:42 +08:00
parent 7f4527d501
commit b010f82221
139 changed files with 2772 additions and 103 deletions

View File

@@ -0,0 +1,3 @@
module go-select-practice
go 1.22.2

View File

@@ -0,0 +1,33 @@
package main
import (
"fmt"
"time"
)
func main() {
ch1 := make(chan string)
ch2 := make(chan string)
// 模拟两个不同来源的“数据源”
go func() {
time.Sleep(1 * time.Second)
ch1 <- "result from ch1 (1s)"
}()
go func() {
time.Sleep(2 * time.Second)
ch2 <- "result from ch2 (2s)"
}()
fmt.Println("等待 ch1 或 ch2 的结果...(谁先来处理谁)")
select {
case v := <-ch1:
fmt.Println("收到 ch1:", v)
case v := <-ch2:
fmt.Println("收到 ch2:", v)
}
fmt.Println("main 结束")
}

View File

@@ -0,0 +1,38 @@
package main
import (
"context"
"fmt"
"time"
)
// 模拟一个可被取消的操作
func doWork(ctx context.Context) error {
for i := 1; i <= 5; i++ {
select {
case <-ctx.Done():
// context 被取消或超时
fmt.Println("doWork 被取消:", ctx.Err())
return ctx.Err()
default:
fmt.Println("工作中 step", i)
time.Sleep(1 * time.Second)
}
}
fmt.Println("doWork 正常完成")
return nil
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
fmt.Println("开始工作,最长 3 秒...")
if err := doWork(ctx); err != nil {
fmt.Println("结束,原因:", err)
return
}
fmt.Println("结束: 正常完成")
}

View File

@@ -0,0 +1,33 @@
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
go func() {
for i := 1; i <= 5; i++ {
time.Sleep(500 * time.Millisecond)
ch <- i
}
close(ch)
}()
for {
select {
case v, ok := <-ch:
if !ok {
fmt.Println("channel 已关闭,退出循环")
return
}
fmt.Println("收到:", v)
default:
// 没有数据可读时,做点“其他事”
fmt.Println("没有新数据,先忙点别的...")
time.Sleep(200 * time.Millisecond)
}
}
}

View File

@@ -0,0 +1,46 @@
package main
import (
"errors"
"fmt"
"time"
)
// 模拟一个可能很慢的操作
func slowOperation() (string, error) {
time.Sleep(3 * time.Second) // 假设真的很慢
return "slow result", nil
}
func doWithTimeout(timeout time.Duration) (string, error) {
resultCh := make(chan string, 1)
errCh := make(chan error, 1)
go func() {
res, err := slowOperation()
if err != nil {
errCh <- err
return
}
resultCh <- res
}()
select {
case res := <-resultCh:
return res, nil
case err := <-errCh:
return "", err
case <-time.After(timeout):
return "", errors.New("操作超时")
}
}
func main() {
fmt.Println("开始调用,最大等待 2 秒...")
res, err := doWithTimeout(2 * time.Second)
if err != nil {
fmt.Println("失败:", err)
return
}
fmt.Println("成功:", res)
}