```
feat(userapp): 添加用户管理功能模块 - 新增models包,包含User结构体和Base基础结构体 - 实现NewUser构造函数用于创建用户实例 - 添加utils包,提供邮箱和年龄验证工具函数 - 在main.go中集成用户创建和验证逻辑 - 添加包初始化函数init()处理包加载顺序 - 新增README.md文档说明各包功能 ```
This commit is contained in:
parent
648f178f8d
commit
6dbefb59ff
@ -1,7 +1,37 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
// userapp 是工程名称
|
||||
// models 是 userapp 下的一个包,包含了用户相关的结构体和函数
|
||||
//如果models 下面还有其他包,那么需要在这里继续导入 userapp/models/user, models/user 才是才是包名称
|
||||
// 导入顺序 models.init(userapp/models/user.go init函数) --> common.init(userapp/models/common/base.go init函数)
|
||||
// init 执行顺序 models.init <-- common.init
|
||||
"userapp/models"
|
||||
|
||||
// utils 包包含了一些工具函数
|
||||
// utils.init(userapp/utils/validators.go init函数)
|
||||
"userapp/utils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("hello user app")
|
||||
|
||||
// 创建用户
|
||||
user := models.NewUser("张三", "zhangsan@example.com", 25)
|
||||
user.ID = 1
|
||||
|
||||
if ok := utils.ValidateEmail(user.Email); !ok {
|
||||
fmt.Println("邮箱格式错误")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if ok := utils.ValidateAge(user.Age); !ok {
|
||||
fmt.Println("年龄格式错误")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Printf("用户创建成功:%+v\n", user)
|
||||
}
|
||||
|
||||
3
day06/userapp/models/README.md
Normal file
3
day06/userapp/models/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# 用户模型
|
||||
|
||||
structs
|
||||
13
day06/userapp/models/common/base.go
Normal file
13
day06/userapp/models/common/base.go
Normal file
@ -0,0 +1,13 @@
|
||||
package common
|
||||
|
||||
type Base struct {
|
||||
ID int `json:"id"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
// init 函数会在包被导入时自动调用,可以用来进行一些初始化操作
|
||||
// 执行顺序: stack 结构, 最后的包 最先被执行, 只有子包的init了,才能执行父包的init
|
||||
func init() {
|
||||
println("common 包的 init 函数被调用")
|
||||
}
|
||||
30
day06/userapp/models/user.go
Normal file
30
day06/userapp/models/user.go
Normal file
@ -0,0 +1,30 @@
|
||||
package models
|
||||
|
||||
// userapp 是工程名称
|
||||
// models 是 userapp 下的一个包,包含了用户相关的结构体和函数
|
||||
// common 是 models 下的一个包,包含了 Base 结构体
|
||||
import (
|
||||
"fmt"
|
||||
"userapp/models/common"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
// 嵌套 common.Base 结构体,包含 ID、CreatedAt 和 UpdatedAt 字段
|
||||
common.Base
|
||||
|
||||
Name string
|
||||
Email string
|
||||
Age int
|
||||
}
|
||||
|
||||
func NewUser(name, email string, age int) *User {
|
||||
return &User{
|
||||
Name: name,
|
||||
Email: email,
|
||||
Age: age,
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
fmt.Println("models 包的 init 函数被调用")
|
||||
}
|
||||
2
day06/userapp/utils/README.md
Normal file
2
day06/userapp/utils/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# 工具函数
|
||||
|
||||
32
day06/userapp/utils/validators.go
Normal file
32
day06/userapp/utils/validators.go
Normal file
@ -0,0 +1,32 @@
|
||||
// Copyright 2024 The Cockroach Authors.
|
||||
//
|
||||
// Use of this software is governed by the Business Source License
|
||||
// included in the file licenses/BSL.txt.
|
||||
//
|
||||
// As of the Change Date specified in that file, in accordance with
|
||||
// the Business Source License, use of this software will be governed
|
||||
// by the Apache License, Version 2.0, included in the file
|
||||
// licenses/APL.txt.
|
||||
//
|
||||
// Package utils contains utility functions.
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ValidateEmail 验证邮箱格式
|
||||
func ValidateEmail(email string) bool {
|
||||
return strings.Contains(email, "@")
|
||||
}
|
||||
|
||||
// ValidateAge 验证年龄范围
|
||||
func ValidateAge(age int) bool {
|
||||
return age > 0 && age < 150
|
||||
}
|
||||
|
||||
func init() {
|
||||
fmt.Println("utils 包的 init 函数被调用")
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user