From 6dbefb59ff219b09ce0fd4bad8a945302a1c9785 Mon Sep 17 00:00:00 2001 From: yumaojun03 <719118794@qq.com> Date: Sun, 1 Mar 2026 11:10:28 +0800 Subject: [PATCH] =?UTF-8?q?```=20feat(userapp):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=E6=A8=A1?= =?UTF-8?q?=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增models包,包含User结构体和Base基础结构体 - 实现NewUser构造函数用于创建用户实例 - 添加utils包,提供邮箱和年龄验证工具函数 - 在main.go中集成用户创建和验证逻辑 - 添加包初始化函数init()处理包加载顺序 - 新增README.md文档说明各包功能 ``` --- day06/userapp/main.go | 32 ++++++++++++++++++++++++++++- day06/userapp/models/README.md | 3 +++ day06/userapp/models/common/base.go | 13 ++++++++++++ day06/userapp/models/user.go | 30 +++++++++++++++++++++++++++ day06/userapp/utils/README.md | 2 ++ day06/userapp/utils/validators.go | 32 +++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 day06/userapp/models/README.md create mode 100644 day06/userapp/models/common/base.go create mode 100644 day06/userapp/models/user.go create mode 100644 day06/userapp/utils/README.md create mode 100644 day06/userapp/utils/validators.go diff --git a/day06/userapp/main.go b/day06/userapp/main.go index 7572f0e..e2a2790 100644 --- a/day06/userapp/main.go +++ b/day06/userapp/main.go @@ -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) } diff --git a/day06/userapp/models/README.md b/day06/userapp/models/README.md new file mode 100644 index 0000000..15d91ab --- /dev/null +++ b/day06/userapp/models/README.md @@ -0,0 +1,3 @@ +# 用户模型 + +structs \ No newline at end of file diff --git a/day06/userapp/models/common/base.go b/day06/userapp/models/common/base.go new file mode 100644 index 0000000..1f67bcf --- /dev/null +++ b/day06/userapp/models/common/base.go @@ -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 函数被调用") +} diff --git a/day06/userapp/models/user.go b/day06/userapp/models/user.go new file mode 100644 index 0000000..71ebe87 --- /dev/null +++ b/day06/userapp/models/user.go @@ -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 函数被调用") +} diff --git a/day06/userapp/utils/README.md b/day06/userapp/utils/README.md new file mode 100644 index 0000000..ee2b84a --- /dev/null +++ b/day06/userapp/utils/README.md @@ -0,0 +1,2 @@ +# 工具函数 + diff --git a/day06/userapp/utils/validators.go b/day06/userapp/utils/validators.go new file mode 100644 index 0000000..f2cf6d8 --- /dev/null +++ b/day06/userapp/utils/validators.go @@ -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 函数被调用") +}