How Should Interfaces Be Named in Go?

Mary-Kate Olsen
Release: 2024-11-15 08:16:03
Original
504 people have browsed it

How Should Interfaces Be Named in Go?

Interface Naming Convention in Go

Naming Conventions for Single-Method Interfaces

In Go, it is conventional to name interfaces with a single method by appending the method name with the suffix "-er" or a similar modification. This results in an agent noun, as seen in interfaces like Reader, Writer, Formatter, and CloseNotifier.

Other Naming Considerations

For interfaces that define multiple methods, it is recommended to choose a name that accurately describes their purpose, such as net.Conn, http.ResponseWriter, or io.ReadWriter.

Naming Conventions for Receiver Types

Avoid using generic names like this or self for receiver types. It is better to use a concise abbreviation that reflects the receiver type, typically one or two characters. For example, c or cl for Client, and r for Rectangle. This approach ensures consistency across the type's methods and emphasizes the receiver's role as an obvious argument.

Application to the Provided Code

In the provided code, it would be appropriate to consider the following interface names:

  • RoleChecker: Interface defining the IsRole method
  • RoleAssumer: Interface defining the AssumeRole method
  • RoleHelper: Combined interface for both IsRole and AssumeRole methods

For the ServerSession interface, ServerSession or Session would be suitable names.

The above is the detailed content of How Should Interfaces Be Named in Go?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template