dongshou9343 2016-08-09 05:05
浏览 170
已采纳

接口命名约定Golang

I'll just post my code:

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

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

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"?

Any suggestions?

  • 写回答

3条回答 默认 最新

  • dongzhong8834 2016-08-09 06:14
    关注

    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
    }
    

    Thank you Sarathsp for getting me thinking about this properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能