Go语言的命名规则是其简洁哲学和工程实用性的集中体现。下面从语法规范、最佳实践到实际应用进行全面解析:
// 小驼峰式(lowerCamelCase)
var userName string
var maxRetryCount = 3
var isConnected bool
特殊场景:
// 短生命周期变量用缩写
i := 0 // 索引
n := len(items) // 数量
ctx := context.Background()
// 大驼峰式(UpperCamelCase)
const MaxConnections = 100
const DefaultTimeout = 5 * time.Second
枚举常量:
const (
StatusPending = iota
StatusProcessing
StatusCompleted
)
// 公共函数:大驼峰
func CalculateTotal() int { /*...*/ }
// 私有函数:小驼峰
func validateInput() error { /*...*/ }
返回值增强:
// 返回布尔值:Is/Has/Can 前缀
func IsValid() bool
// 返回错误:Err 后缀
func Parse() error
// 名词性 + 实体性
type UserProfile struct { /*...*/ }
type HTTPRequest struct { /*...*/ }
type DatabaseConfig struct { /*...*/ }
// 避免动词命名(错误示例)
type ProcessData struct {} // 不推荐
// 行为抽象:以 -er 后缀
type Reader interface {
Read(p []byte) (n int, err error)
}
type Stringer interface {
String() string
}
// 多方法接口
type OrderProcessor interface {
Validate() error
Process() (string, error)
}
// 明确语义的别名
type UserID string
type Timestamp int64
// 方法增强
func (uid UserID) IsValid() bool {
return len(uid) > 0
}
# 目录结构
services/
├── user/ # 目录名
│ └── service.go # 包名:user
storage/
├── mysql/ # 目录名
│ └── store.go # 包名:mysql
关键规则:
import (
"database/sql"
json "encoding/json" // 标准库别名
mongo "go.mongodb.org/mongo-driver/mongo" // 第三方别名
)
type User struct {
name string
}
// 避免冗余的Get前缀
func (u *User) Name() string {
return u.name
}
// Setter带参数名
func (u *User) SetName(name string) {
u.name = name
}
// user_test.go
package user_test // 测试包
func TestUserCreation(t *testing.T) {
u := NewUser("Alice") // 测试公共API
// ...
}
// 类型首字母缩写(1-2字母)
func (u *User) Update() {}
func (c *Client) Send() {}
// 一致性优先
func (db *Database) Query() {}
func (srv *HTTPServer) Start() {}
类型 | 推荐长度 | 示例 | 说明 |
---|---|---|---|
局部变量 | ≤5字符 | user , count |
上下文明确 |
包级变量 | 5-10字符 | maxRetries , logger |
作用域广需明确 |
函数参数 | 3-8字符 | ctx , req , opts |
结合类型信息 |
接口方法 | 1-2单词 | Read , WriteTo |
动词短语 |
错误变量 | Err前缀 | ErrTimeout |
全局错误变量 |
type Logger struct { /*...*/ }
// 添加后缀避免冲突
type FileLogger struct {
// 包含Logger
}
import (
"net/http"
http2 "custom/http" // 自定义别名
)
type Client struct {
timeout time.Duration
}
// 方法使用完整名
func (c *Client) RequestTimeout() time.Duration {
return c.timeout
}
// 路由定义
router.POST("/users", user.Handler.CreateUser)
// 分层架构
services/
├── user/
│ ├── service.go // user.Service
│ └── handler.go // user.Handler
storage/
├── postgres/
│ ├── user_store.go // postgres.UserStore
// user_service.proto
service UserService {
rpc GetUser (GetUserRequest) returns (User);
}
message GetUserRequest {
string user_id = 1;
}
message User {
string name = 1;
string email = 2;
}
# golangci-lint 检查
golangci-lint run --enable=revive
# .golangci.yml
linters-settings:
revive:
rules:
- name: exported
arguments: [ [ "Stutter", "Error" ] ]
- name: receiver-naming
⚠️ 命名警告:Interface type name 'Clienter' should end with 'er'
✅ 正确命名:type Client interface
⚠️ 命名警告:method name 'UpdateUserName' should not contain the type name 'User'
✅ 正确命名:func (u *User) UpdateName()
Url
→ 现在:URL
Json
→ 现在:JSON
Ip
→ 现在:IP
log
而非 logs
util
→ stringutil/timeutil
buf
替代 buffer
userCount
)CalculateTotal()
)Reader
)User.UserName
)TestUser_Create_InvalidEmail
)终极法则:让代码像自然语言一样可读。好的命名应使注释变得多余,直接传达设计意图和业务语义。
通过遵循这些规则,开发者可以创建出符合Go语言哲学、具有良好可维护性的代码库,显著降低团队协作成本。