接口命名约定Golang [英] Interface naming convention Golang

查看:88
本文介绍了接口命名约定Golang的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将发布我的代码:

/*
*  Role will ALWAYS reserve the session key "role".
 */
package goserver

const (
    ROLE_KEY string = "role"
)

type Role string

//if index is higher or equal than role, will pass
type RolesHierarchy []Role

func (r Role) String() string {
    return string(r)
}

func NewRole(session ServerSession) Role {
    return session.GetValue(ROLE_KEY).(Role)
}

func (this Role) IsRole(role Role, hierarchy RolesHierarchy) bool {
    if role == this {
        return true
    }
    if len(hierarchy) == 0 {
        return false
    }
    var thisI int = 0
    var roleI int = 0
    //Duped roles in hierarchy are verified in verifyConfig during parse
    for i, r := range hierarchy {
        if this == r {
            thisI = i
        }
        if role == r {
            roleI = i
        }
    }
    //TODO I can probably condense what follows into one if
    if thisI == 0 && roleI == 0 {
        return false
    }
    return thisI >= roleI
}

func (this *Role) AssumeRole(session ServerSession, role Role) {
    session.SetValue(ROLE_KEY, role)
    *this = role
}

应该注意,ServerSession也是一个接口,对我来说,调用"ServerSessioner"简直太不可思议了.

It should be noted that ServerSession is also an interface, calling "ServerSessioner" just feels wonky to me.

我正在玩用IsRole()和AssumeRole()创建接口的想法,但是"Roler"似乎很奇怪.我突然意识到,除了标准的"er"后缀之外,我真的不知道或者从未遇到过接口的命名约定.我似乎确实记得VS C ++约定是在所有内容前面都加一个"I".这是惯用的"吗?

I'm toying with the idea of creating an interface with IsRole() and AssumeRole(), however "Roler" seems very wonky. It's dawning on me that I don't really know or have ever come across naming conventions for interfaces, other than the standard "er" suffix. I do seem to recall the VS C++ convention is to just throw an "I" in front of everything. Is this "idiomatic"?

有什么建议吗?

推荐答案

我明白了,事实证明我可以使用"er"约定.

I got it, turns out I can use the "er" convention.

/*
*  Role will ALWAYS reserve the session key "role".
 */
package goserver

const (
    ROLE_KEY string = "role"
)

type Role string

//if index is higher or equal than role, will pass
type RolesHierarchy []Role

type RoleChecker interface {
    IsRole(Role, RolesHierarchy) bool
}

type RoleAssumer interface {
    AssumeRole(ServerSession, Role)
}

type RoleCheckerAssumer interface {
    RoleChecker
    RoleAssumer
}

func (r Role) String() string {
    return string(r)
}

func NewRole(session ServerSession) Role {
    return session.GetValue(ROLE_KEY).(Role)
}

func (this Role) IsRole(role Role, hierarchy RolesHierarchy) bool {
    if role == this {
        return true
    }
    if len(hierarchy) == 0 {
        return false
    }
    var thisI int = 0
    var roleI int = 0
    //Duped roles in hierarchy are verified in verifyConfig during parse
    for i, r := range hierarchy {
        if this == r {
            thisI = i
        }
        if role == r {
            roleI = i
        }
    }
    //TODO I can probably condense what follows into one if
    if thisI == 0 && roleI == 0 {
        return false
    }
    return thisI >= roleI
}

func (this *Role) AssumeRole(session ServerSession, role Role) {
   session.SetValue(ROLE_KEY, role)
   *this = role
}

感谢Sarathsp让我正确地考虑了这一点.

Thank you Sarathsp for getting me thinking about this properly.

这篇关于接口命名约定Golang的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆