
go 语言接口和实现的命名指南
go 语言对于接口和其实现的命名规范没有严格规定。然而,有几个惯例可以提高代码的可读性和可维护性。
接口命名
- 大写开头的单词。
- 避免使用动词或形容词。
- 尽量使用与接口用途一致的名称。
实现命名
- 与相应的接口同名,但使用小写字母开头。
- 可选地,可以在实现名称后面添加“impl”后缀。
例如:
type UserService interface {
SignupByEmail(ctx *gin.Context, user domain.User) error
LoginByEmail(ctx *gin.Context, user domain.User) (domain.User, error)
}
type userServiceImp struct {
userRepo repo.UserRepository
}
func NewUserServiceImp(userRepo repo.UserRepository) *userServiceImp {
return &userServiceImp{userRepo: userRepo}
}对于仓库和 dao,可以遵循类似的命名约定。
有关最佳实践的更多信息,建议参考 google 的 go 最佳实践网站。










