Home > Backend Development > Golang > How should I name interfaces in Go?

How should I name interfaces in Go?

Patricia Arquette
Release: 2024-11-11 02:14:03
Original
699 people have browsed it

How should I name interfaces in Go?

Interface Naming Convention: Focus on Purpose and Consistency

In Go, interface names are typically chosen based on their intended purpose and clarity, rather than strict adherence to specific conventions.

For interfaces that define a single method, the naming scheme often follows the format:

[Method Name]er
Copy after login

For example, an interface defining a Read method would be named Reader. However, there may be exceptions where using a pure English word or phrase makes more sense, such as StringBuilder or ByteReader.

When defining interfaces with multiple methods, it's crucial to choose a name that accurately reflects their purpose, such as net.Conn, http.ResponseWriter, or io.ReadWriter.

Regarding receiver names, it's advised to use concise one or two-letter abbreviations that reflect the type. For example, using client would be a suitable name for a receiver of type Client.

It's important to note that using prefixes like I or suffixes like er is generally not considered idiomatic in Go when naming interfaces. These conventions are more commonly associated with other programming languages.

In your specific case, suggested interface names include:

  • RoleChecker
  • RoleAssumer
  • RoleCheckerAssumer

For a single interface with both methods, consider naming it RoleHelper or RoleChecker.

As for ServerSession, the name Session would be sufficient, or even Sessioner if desired. Avoid using verbose names like ServerSessioner.

The above is the detailed content of How should I name interfaces 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