go - 将非接口(interface)称为 "*er"是惯用的吗

标签 go

"Effective Go"状态:

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.

bufio.io 包包含这个:

// Reader implements buffering for an io.Reader object.
type Reader struct {
    buf          []byte
    rd           io.Reader
    r, w         int
    err          error
    lastByte     int
    lastRuneSize int
}

将结构命名为“*er”是惯用的吗?特别是在这种情况下,它是一个与 io.Reader 同名的 struct,它是一个接口(interface)。

最佳答案

如果它不在 Effective Go 或规范中,那么这真的是一个见仁见智的问题,我会说只要它有意义就可以。

bufio.Readerbytes.Reader 为例,以这种方式命名它们非常有意义。

关于go - 将非接口(interface)称为 "*er"是惯用的吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24536157/

相关文章:

Docker 抛出 "{binary} not found"错误

go - Go中函数的 channel 返回类型中的 `<-`是什么意思?

go - 为什么在这种情况下 go 不报告 "slice bounds out of range"?

GoLang printf 什么都不显示,即使在使用 time.Sleep 之后

go - 为什么从代码中调用 Setenv 不起作用?

go - Go 中的路由器 - 在每个 http 请求之前运行一个函数

encoding - go.text 中的 iso-8859-1 编码支持

performance - 协程性能

concurrency - Go并发和 channel 困惑

performance - 在golang中解析日期和时间的最佳方式