Why a nil error returned from strings.Builder WriteString in golang, is it necessary?

huangapple go评论66阅读模式
英文:

Why a nil error returned from strings.Builder WriteString in golang, is it necessary?

问题

当我审查同事的代码时,我发现一个被忽略的返回值err,尽管通常情况下我们不会这样做:

b := new(strings.Builder)
b.WriteString("Hello, World!") // 忽略了err

WriteString的源代码声明可能会返回一个错误,但实际上它永远不会返回错误(始终将错误值返回为nil):

// WriteString将s的内容附加到b的缓冲区。
// 它返回s的长度和一个nil错误。
func (b *Builder) WriteString(s string) (int, error) {
    b.copyCheck()
    b.buf = append(b.buf, s...)
    return len(s), nil
}

如果按照以下方式移除错误返回,可能会出现什么问题?

func (b *Builder) WriteString(s string) int {
    b.copyCheck()
    b.buf = append(b.buf, s...)
    return len(s)
}
英文:

When reviewed my colleague's code, I found that a returned err has been ignored, though we would not do that in general:

b := new(strings.Builder)
b.WriteString("Hello, World!") // ignore err

The source code for WriteString declares it may return an error, but in fact it never will (always returning nil for the error value):

// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func (b *Builder) WriteString(s string) (int, error) {
	b.copyCheck()
	b.buf = append(b.buf, s...)
	return len(s), nil
}

What would the issues be, if any, with removing the error return, as follows?

func (b *Builder) WriteString(s string) int {
	b.copyCheck()
	b.buf = append(b.buf, s...)
	return len(s)
}

答案1

得分: 4

这是要翻译的内容:

引入strings.Builder变更列表中包含了很多关于使该API与bytes.Buffer类似的注释。

例如,

> 毕竟,这就是bytes.Buffer的行为方式,而我们应该是bytes.Buffer的子集。

查看一些bytes.Buffer函数的文档,它提到

> WriteRune将Unicode代码点r的UTF-8编码附加到缓冲区,返回其长度和一个错误,该错误始终为nil,但包含在其中以匹配bufio.Writer的WriteRune。

看起来他们基本上是在设计一个与Golang标准库中的其他接口类似的API。尽管始终为nil的错误是多余的,但它允许Builder与接受bytes.Bufferbufio.Writer的现有接口匹配。其中一个接口是io.StringWriter,它看起来像是这样的:

type StringWriter interface {
	WriteString(s string) (n int, err error)
}

这里的err返回值很有用,因为其他的StringWriter实现可能会返回错误。

英文:

The changelist which introduces strings.Builder includes a lot of comments about trying to make this API similar to bytes.Buffer.

For instance,

> That's how a bytes.Buffer behaves, after all, and we're supposed to be a subset of a bytes.Buffer.

Looking at the documentation for some bytes.Buffer functions, it mentions

> WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer, returning its length and an error, which is always nil but is included to match bufio.Writer's WriteRune.

It looks like they're basically trying to design an API that's similar to other interfaces in Golang's standard library. Even though the always-nil error is redundant, it allows the Builder to match existing interfaces that would accept bytes.Buffer or bufio.Writer. One such interface is io.StringWriter, which looks like

type StringWriter interface {
	WriteString(s string) (n int, err error)
}

The err return value here is useful since other StringWriter implementations could possibly return errors.

答案2

得分: 0

在Go语言中,返回一个值和错误是非常常见的。因此,你可以检查错误是否为空,如果没有错误,就可以轻松使用返回的值。
换句话说,如果从一个函数中接收到错误,那么就表示函数调用出现了问题。

英文:

Go, it's quite common to return a value and error. So you can check the error is not null, if no error then easily use the returned value.
In other words, if it receives an error from a function then it indicates there was a problem with the function called.

huangapple
  • 本文由 发表于 2021年12月17日 11:48:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/70388206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定