[英]multiple-value in single-value context no return func
I have a func in Go that simply writes to a buffer.我在 Go 中有一个 func,它只是写入缓冲区。 I have no return type set on the func so I am not sure why I am seeing this error.
我没有在 func 上设置返回类型,所以我不确定为什么会看到这个错误。 Here is my code:
这是我的代码:
func Write(buffer *bytes.Buffer, values ...string) {
for _, val := range values
_, err := *buffer.WriteString(val)
if err != nil {
// print error
}
}
_, err := *buffer.WriteString(" ")
if err != nil {
// print error
}
}
It complains at both lines where I have buffer.WriteString.它在我有 buffer.WriteString 的两行都抱怨。 This leads me to believe it has something to do with the return types of the WriteString method on the buffer but I am not experienced enough in Go to know for sure.
这让我相信它与缓冲区上 WriteString 方法的返回类型有关,但我对 Go 的经验不足,无法确定。
Any help would be appreciated.任何帮助,将不胜感激。
Edit: Updated code.编辑:更新代码。
You don't need to dereference pointers to call methods in Go.你不需要取消引用指针来调用 Go 中的方法。 The
*
operator before buffer.WriteString
is applied to the returned values. buffer.WriteString
之前的*
运算符应用于返回值。 To dereference buffer
you would need to write (*buffer).WriteString
, but that's not needed at all:要取消引用
buffer
您需要编写(*buffer).WriteString
,但这根本不需要:
func Write(buffer *bytes.Buffer, values ...string) {
for _, val := range values {
_, err := buffer.WriteString(val)
if err != nil {
// print error
}
}
_, err := buffer.WriteString(" ")
if err != nil {
// print error
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.