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 函数被调用") +}