- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我将发布我的代码:
/*
* 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也是一个接口(interface),调用“ServerSessioner”对我来说感觉很奇怪。
我正在玩弄用 IsRole() 和 AssumeRole() 创建接口(interface)的想法,但是“Roler”似乎很不稳定。我突然意识到,除了标准的“er”后缀之外,我真的不知道或曾经遇到过接口(interface)的命名约定。我似乎确实记得 VS C++ 的约定是在所有内容前面加上一个“I”。这是“惯用语”吗?
有什么建议吗?
最佳答案
在您的情况下,我将它们命名为 RoleChecker
和 RoleAssumer
,即“合并”的一个 RoleCheckerAssumer
。或者,如果您使用单个界面,则可能是 RoleHelper
或 RoleChecker
。
ServerSession
也可以,甚至只是 Session
(尤其是在没有“客户端” session 的情况下)。另一方面,ServerSessioner
不好,Session
不是动词,也不是接口(interface)的方法。
关于约定的帖子很多。
Effective Go: Interface names:
By convention, one-method interfaces are named by the method name plus an -er suffix or similar modification to construct an agent noun:
Reader
,Writer
,Formatter
,CloseNotifier
etc.There are a number of such names and it's productive to honor them and the function names they capture.
Read
,Write
,Close
,Flush
,String
and so on have canonical signatures and meanings. To avoid confusion, don't give your method one of those names unless it has the same signature and meaning. Conversely, if your type implements a method with the same meaning as a method on a well-known type, give it the same name and signature; call your string-converter methodString
notToString
.
Interface Types @ What's in a name? - Talks at golang.org
Interfaces that specify just one method are usually just that function name with 'er' appended to it.
type Reader interface {
Read(p []byte) (n int, err error)
}Sometimes the result isn't correct English, but we do it anyway:
type Execer interface {
Exec(query string, args []Value) (Result, error)
}Sometimes we use English to make it nicer:
type ByteReader interface {
ReadByte() (c byte, err error)
}When an interface includes multiple methods, choose a name that accurately describes its purpose (examples: net.Conn, http.ResponseWriter, io.ReadWriter).
对于接收者名称,请勿使用 this
或 self
或类似名称。而是:
Receivers @ What's in a name? - Talks at golang.org
Receivers are a special kind of argument.
By convention, they are one or two characters that reflect the receiver type, because they typically appear on almost every line:
func (b *Buffer) Read(p []byte) (n int, err error)
func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request)
func (r Rectangle) Size() PointReceiver names should be consistent across a type's methods. (Don't use r in one method and rdr in another.)
Go Code Review Comments: Receiver Names:
The name of a method's receiver should be a reflection of its identity; often a one or two letter abbreviation of its type suffices (such as "c" or "cl" for "Client"). Don't use generic names such as "me", "this" or "self", identifiers typical of object-oriented languages that place more emphasis on methods as opposed to functions. The name need not be as descriptive as that of a method argument, as its role is obvious and serves no documentary purpose. It can be very short as it will appear on almost every line of every method of the type; familiarity admits brevity. Be consistent, too: if you call the receiver "c" in one method, don't call it "cl" in another.
关于go - 接口(interface)命名约定 Golang,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842457/
我正在尝试运行这段代码,用随机数替换字符串中的一个字符: //Get the position between 0 and the length of the string-1 to insert
我有一个包含 3 个位置的数组,假设它的所有位置都是数字 5。 [5 5 5] 我怎样才能以保持 555 的方式将它传递给 var?就像这样。 n:= 555 最佳答案 与使用任何其他语言的方式相同:
我使用 go dep 工具版本 v0.4.1,现在当我运行 dep init 时它会按预期创建 2 个文件,当我打开 gopkg.lock 我发现例如以下内容 [[projects]] name
我正在制作学习联系申请。我有一个 NewContact()。 // Contact - defines the fields of an entire Contact type Contact str
我一直在尝试使用该模块: https://godoc.org/github.com/hirochachacha/go-smb2#RemoteFile.ReadAt 为了在 Windows 机器上对我的
我需要在 golang 中编译 golang 中的程序。有没有不使用 exec.Command("go","build") 的原生形式? 最佳答案 不幸的是,我认为使用 exec.Command 是利
编写输出有效 go 代码的 go 应用程序可能最好使用内置的“go”包及其一些子包(“go/ast”、“go/token”、“go/printer”、等)。 要创建字符串文字表达式,您需要创建一个 a
我正在尝试使用 Golang 和 gin 为我的 api 和前端编写代理。如果请求转到除“/api”之外的任何内容,我想代理到 svelte 服务器。如果出现“/api/something”,我想在
我偶然发现了这个博客:using go as a scripting language并尝试创建一个可用于运行 golang 脚本的自定义图像,即 FROM golang:1.15 RUN go ge
我刚开始接触golang,我需要从json字符串中获取数据。 {"data" : ["2016-06-21","2016-06-22","2016-06-25"], "sid" : "ab", "di
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 3 年前。 Improve
我是 goland 的新手,试图在我的第一个项目中使用它。我注意到在 goland 中它没有显示通过容器引入的相同 golang SDK。 这是我的 Dockerfile: FROM golang:1
我正在试用 golang-neo4j-bolt-driver 包 github.com/johnnadratowski/golang-neo4j-bolt-driver 我已经导入了包并正在使用创建新
如果我安装了Go发行版软件包,则会在/usr/lib/golang/pkg中看到很多文件,在/usr/lib/golang/src中看到非常相似的文件集。这两组之间有什么关系? pkg是从src中的源
我发现 golang 上下文对于在客户端-服务器请求范围内取消服务器的处理很有用。 我可以使用 http.Request.WithContext 方法发出带有上下文的 http 请求,但是如果客户端不
我正在尝试将一个 golang 数组(还有 slice、struct 等)放置到 HTML 中,这样当从 golang gin web 框架返回 HTML 时,我可以在 HTML 元素内容中使用数组元
目前正在使用这个 ffmpeg 命令编辑视频 ffmpeg -i "video1.ts" -c:v libx264 -crf 20 -c:a aac -strict -2 "video1-fix.ts
我需要从 play.golang.org 链接读取 golang 代码并保存到 .go 文件。我想知道 play.golang.org 是否有任何公共(public) API 支持。我用谷歌搜索但没有
我第一次使用 IntelliJ 的最新 (2014-01-03) Golang 插件。 通常,我的终端工作流程是 go build && ./executable -args=1 所以我试图创建一个启
这个问题只是在构建之间随机出现,现在甚至我们的生产 repo,几个月都没有改变,在构建时也会出现这个问题。我已经坚持了一段时间。它不会发生在我们的本地机器上,只有在使用 dockerfile 时才会发
我是一名优秀的程序员,十分优秀!