在Go语言中,io软件包为I /O原语提供基本接口。它的主要工作是封装此类原始之王的正在进行的实现。 Go语言中的PipeWriter.CloseWithError()函数用于关闭编写器。但是,从PipeReader进行连续读取,即,读取一半的管道将不返回任何字节,并且如果错误为nil,则返回错误err或EOF错误。而且,此函数在io包下定义。在这里,您需要导入“io”包才能使用这些函数。
用法:
func (w *PipeWriter) CloseWithError(err error) error
在这里,“w”是指向PipeWriter的指针。其中PipeWriter是管道的写入部分,而“err”是我们在代码中指出的错误(如果发生错误,将在打印的代码中显示)。
返回值:如果有错误,则返回错误,否则返回EOF。
注意:如果此方法存在,并且永远返回“nil”,则永远不会重写该方法。
范例1:
// Golang program to illustrate the usage of
// io.PipeWriter.CloseWithError() function
// Including main package
package main
// Importing fmt and io
import (
"fmt"
"io"
)
// Calling main
func main() {
// Calling Pipe method
pipeReader, pipeWriter:= io.Pipe()
// Defining data parameter of Read method
data:= make([]byte, 20)
// Reading data into the buffer stated
go func() {
pipeReader.Read(data)
// Calling CloseWithError() method with
// its parameter
pipeReader.CloseWithError(fmt.Errorf("There is an "+
"error in the code written!"))
}()
// Using for loop
for i:= 0; i < 1; i++ {
// Calling pipeWriter.Write() method
n, err:= pipeWriter.Write([]byte("GeeksforGeeks!"))
// If error is not nil panic
if err != nil {
panic(err)
}
// Prints the content written
fmt.Printf("%v\n", string(data))
// Prints the number of bytes
fmt.Printf("%v\n", n)
}
}
输出:
GeeksforGeeks! 14
此处,未返回错误,因为此处未调用CloseWithError()方法,因为循环仅在一次读操作后停止。
范例2:
// Golang program to illustrate the usage of
// io.PipeWriter.CloseWithError() function
// Including main package
package main
// Importing fmt and io
import (
"fmt"
"io"
)
// Calling main
func main() {
// Calling Pipe method
pipeReader, pipeWriter:= io.Pipe()
// Defining data parameter of Read method
data:= make([]byte, 20)
// Reading data into the buffer stated
go func() {
pipeReader.Read(data)
// Calling CloseWithError() method with
// its parameter
pipeReader.CloseWithError(fmt.Errorf("There is"+
" an error in the code written!"))
}()
// Using for loop
for i:= 0; i < 2; i++ {
// Calling pipeWriter.Write() method
n, err:= pipeWriter.Write([]byte("GeeksforGeeks!"))
// If error is not nil panic
if err != nil {
panic(err)
}
// Prints the content written
fmt.Printf("%v\n", string(data))
// Prints the number of bytes
fmt.Printf("%v\n", n)
}
}
输出:
GeeksforGeeks! 14 panic:There is an error in the code written! goroutine 1 [running]: main.main() /tmp/sandbox246824679/prog.go:39 +0x3c2
此处,由于“for”循环在第二次迭代后停止,因此调用CloseWithError()方法时将引发错误。而这里返回的错误就是我们所说的。
相关用法
- Golang math.Lgamma()用法及代码示例
- Golang math.Float64bits()用法及代码示例
- Golang atomic.AddInt64()用法及代码示例
- Golang atomic.StoreInt64()用法及代码示例
- Golang reflect.FieldByIndex()用法及代码示例
- Golang string.Contains用法及代码示例
- Golang bits.Sub()用法及代码示例
- Golang time.Round()用法及代码示例
- Golang reflect.AppendSlice()用法及代码示例
- Golang reflect.ChanOf()用法及代码示例
- Golang flag.Bool()用法及代码示例
- Golang time.Sleep()用法及代码示例
- Golang time.Time.Year()用法及代码示例
- Golang reflect.DeepEqual()用法及代码示例
- Golang reflect.Indirect()用法及代码示例
- Golang reflect.CanAddr()用法及代码示例
- Golang reflect.CanInterface()用法及代码示例
- Golang reflect.CanSet()用法及代码示例
- Golang reflect.Cap()用法及代码示例
- Golang strings.ContainsRune()用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 io.PipeWriter.CloseWithError() Function in Golang with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。