Golang Interface Naming Convention

I will just send 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 the interface that calls the "ServerSessioner", just feels awkward for me.

I play with the idea of ​​creating an interface with IsRole () and AssumeRole (), however "Roler" seems very awkward. It seems to me that I really don't know or have ever come across name conventions for interfaces other than the standard suffix "er". I seem to remind you that VS C ++ is just throwing an β€œI” in front of everything. Is this "idiomatic"?

Any suggestions?

+5
source share
3 answers

I got it, 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 } 

Thanks to Sarathsp for thinking this right.

+1
source

In your case, I would just call them RoleChecker and RoleAssumer , the "combined" one RoleCheckerAssumer . Or if you go with one interface, it can be RoleHelper or RoleChecker .

ServerSession also ServerSession or even just Session (especially if the client session does not exist). ServerSessioner On the other hand, badly, Session not a verb, not an interface method.


There have been many reports of agreements.

Effective Transition: Interface Names:

By convention, the interfaces of one method are called the name of the method plus the suffix -er or a similar modification to create a noun agent: Reader , Writer , Formatter , CloseNotifier , etc.

There are many such names, and they are productive for their honor and the names of the functions that they capture. Read , Write , Close , Flush , String , etc. have canonical signatures and meanings. To avoid confusion, do not give your method one of these names if it does not have the same signature and value. Conversely, if your type implements a method with the same value as the method of a known type, give it the same name and signature; call the string converter method String not ToString .

Interface Types @ What is called? - Negotiations at golang.org

Interfaces that specify only one method are usually the name of this function with the addition of er.

 type Reader interface { Read(p []byte) (n int, err error) } 

Sometimes the result is incorrect in English, but we still do:

 type Execer interface { Exec(query string, args []Value) (Result, error) } 

Sometimes we use English to make it more pleasant:

 type ByteReader interface { ReadByte() (c byte, err error) } 

When an interface includes several methods, select a name that accurately describes its purpose (examples: net.Conn, http.ResponseWriter, io.ReadWriter).

For recipient names, do not use this or self or similar. Instead of this:

Receivers @ What is called? - Negotiations at golang.org

Receivers are a special argument.

By convention, they are one or two characters that reflect the type of receiver, because they usually 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() Point 

Recipient names must be consistent between type methods. (Do not use r in one method and rdr in another.)

Go Code Review Comments: Recipient Names:

The name of the method receiver should be a reflection of its identity; often one or two letter abbreviations of its type are sufficient (for example, "c" or "cl" for "Client"). Do not use common names such as "me", "this" or "self", identifiers typical of object-oriented languages, which pay more attention to methods rather than functions. The name should not be as descriptive as the name of the argument of the method, since its role is obvious and does not serve any documentary purpose. It can be very short, as it will appear on almost every line of each type method; acquaintance allows brevity. Be also consistent: if you call the receiver "c" in one way, do not call it "cl" in another.

+12
source

In golang By convention, the interface names of one method are nouns denoting the executor of an action. For instance,

 the `Read` method implements the `Reader` interface, and the `Generate` method implements the `Generator` interface. 

It would be better to make clarity of consensus, no matter what they represent. It’s good when in the interface

only one function is required or a very specific set of functions is required.

There is a practice of using the I prefix for the least common denominator of functions, in which case IRole will be the best interface name, since the interface defines two functions that must be satisfied by all types representing Role

+1
source

All Articles