Go语言中接口和实现的命名规范?

作者站长头像
站长
· 阅读数 45

Go对于接口和其实现的命名规范是什么?

假设整个Web项目都依赖于接口,比如service:

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}
}

但是好像对于GO来说一般不像在Java一样在后面加interface或者Imp来标识,之前看到有人是直接以大小写的UserService和userService来区分接口和实现,但是这样的话构造函数就只能返回接口不能返回具体实现了。对于repository也有相同的疑问,对于dao和cache我是直接接口UserDao然后实现UserMysqlDao用具体的dao实现来区别的。有其他更优雅的符合Go的命名吗?

回复
1个回答
avatar
test
2024-06-20

推荐一个谷歌的 go best practices 网站: best-practices, 希望对你有用。

回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容