This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
order-service: | |
build: | |
context: ./ | |
dockerfile: ./build/package/Dockerfile | |
ports: | |
- "8080:8080" | |
- "9090:9090" | |
depends_on: | |
db: |
Understanding Go's backend architecture requires a systematic approach that focuses on patterns rather than individual lines of code. Let's break this down into clear, manageable concepts.
- Layered Architecture - Separate concerns into distinct layers
- Each layer communicates through well-defined interfaces
- Enables independent evolution of components
- Interface-Oriented Design - Define contracts through interfaces 1:1
权限模型 https://github.com/chenyongze/go-api-admin
CREATE TABLE `dt_uc_admin` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`login_name` varchar(20) NOT NULL DEFAULT '' COMMENT '用户名',
`real_name` varchar(32) NOT NULL DEFAULT '0' COMMENT '真实姓名',
在Go中,定义结构体(struct)是一个重要的设计决策。让我们通过实际场景来理解什么时候应该定义一个结构体:
使用结构体封装相关数据
type Photo struct {
ID string
Filename string
Metadata map[string]interface{}
CreatedAt time.Time
}
在Go语言中,方法接收器(receiver)有两种形式:值接收器和指针接收器。让我们详细比较这两种方式:
特性 | 值接收器(h Handler) | 指针接收器(h *Handler) |
---|---|---|
修改能力 | 只能读取,不能修改原值 2:3 | 可以修改原值 3:5 |
内存使用 | 创建副本,消耗更多内存 | 直接操作原始数据,更节省内存 |
方法调用 | 可用于值和指针接收者 | 同样可用于值和指针接收者 0:1 |
适用场景 | 只读操作、计算等不需要修改状态的场景 | 需要修改结构体字段或处理大型数据结构时 |
让我们通过一个图表来直观地理解它们的区别:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/astaxie/build-web-application-with-golang/blob/c294b087b96d99d2593ad8f470151af5354bb57f/zh/01.4.md?plain=1#L1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go install github.com/fzipp/gocyclo/cmd/gocyclo@latest | |
PS D:\GoLandProjects\go\src\level_old002_concept\planet> gocyclo -top 10 . | |
150 tool GetAxleOne toro\tool\appUtil.go:382:1 | |
70 service TpService toro\service\tpService.go:223:1 | |
64 aep querySettingData internal\aep\aepexchange.go:32:1 | |
56 service brakingMessageBraking toro\service\tpService.go:629:1 | |
56 aep ParseMessageCMD internal\aep\aep.go:877:1 | |
56 aep ParseMessage internal\aep\aep.go:161:1 | |
42 tool GetModelIdLocation toro\tool\appUtil.go:103:1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/acking-you/byte_douyin_project.git | |
type UserInfo struct { | |
Id int64 `json:"id" gorm:"id,omitempty"` | |
Name string `json:"name" gorm:"name,omitempty"` | |
FollowCount int64 `json:"follow_count" gorm:"follow_count,omitempty"` | |
FollowerCount int64 `json:"follower_count" gorm:"follower_count,omitempty"` | |
IsFollow bool `json:"is_follow" gorm:"is_follow,omitempty"` | |
User *UserLogin `json:"-"` //用户与账号密码之间的一对一 | |
Videos []*Video `json:"-"` //用户与投稿视频的一对多 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
main.go | |
func seedAccount(store Storage, fname, lname, pw string) *Account { | |
} | |
func seedAccounts(s Storage) { | |
seedAccount(s, "anthony", "GG", "hunter88888") | |
} | |
func main() { | |
seed := flag.Bool("seed", false, "seed the db") |
NewerOlder