我只发布我的代码:

/*
*  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"对我来说是不可靠的.

我正在考虑用IsRole()和AssumeRole()创建一个接口,但是"Roler"似乎非常不可靠.我开始意识到,除了标准的"er"后缀之外,我并不真正了解或曾经遇到过接口的命名约定.我似乎还记得VS C++的惯例是在所有内容前面加上一个"i".这是"惯用语"吗?

有什么建议吗?

推荐答案

我明白了,原来我可以用"呃"惯例.

/*
*  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让我好好考虑这件事.

Go相关问答推荐

如何在Go中使用a-h/tempson渲染预格式的内容?

迭代项列表时如何超时并发调用

Golang Cososdb-gremlin连接

难以为多个平台添加Go Bazel构建选项

理解Golang并发:缓冲通道的意外行为

如何确定泛型类型在运行时是否可比较?

在 Go 中将元数据从一个 JPEG 复制到另一个

通过 Terraform 中的 MapNestedAtribute 进行迭代

htmx 表单 + gin 无法正确读取请求正文

在 Cloud Run 中找不到默认凭据

不能在 *gorm.db 而不是 gorm.db 上使用 WithContext(ctx) 方法

golang pic.ShowImage 为什么它不生成图像而是向我发送base64值

Golang 工作池实现意外工作

使用 Golang SQL 驱动程序连接到snowflake

使用无服务器工作流 go sdk 时出现间歇性 JSON 解组错误

自定义指标未显示在 prometheus web ui 中,grafana 中也是如此

函数调用中的类型参数panic

带有 grpc 的 protobuf 用于拆分包中的 Go

防止在 Go 公用文件夹中列出目录

如何使用通用字段初始化匿名struct数组