在Go語言中,io軟件包為I /O原語提供基本接口。它的主要工作是封裝此類原始之王的正在進行的實現。 Go語言中的PipeWriter.Close()函數用於關閉編寫器。但是,從PipeReader進行連續讀取,即,讀取一半的管道不會返回任何字節,並且會返回EOF錯誤。而且,此函數在io包下定義。在這裏,您需要導入“io”包才能使用這些函數。
用法:
func (w *PipeWriter) Close() error
在這裏,“w”是指向PipeWriter的指針。其中PipeWriter是管道的寫入部分。
返回值:它返回在Close方法之前寫入的內容。並且從PipeReader進行的連續讀取(即,讀取一半的管道)在調用Close()方法之後將不返回任何字節,並且將返回EOF錯誤。
範例1:
// Golang program to illustrate the usage of
// io.PipeWriter.Close() function
// Including main package
package main
// Importing fmt and io
import (
"fmt"
"io"
)
// Calling main
func main() {
// Calling Pipe method
pipeReader, pipeWriter:= io.Pipe()
// Calling Close method in go function after
// two Write operations
go func() {
pipeWriter.Write([]byte("GfG"))
pipeWriter.Write([]byte("GeeksforGeeks"))
pipeWriter.Close()
// Again calling Write method
pipeWriter.Write([]byte("GfG is a CS-Portal."))
}()
// Creating buffer using make keyword
// of specified length
buffer:= make([]byte, 50)
// Using for loop
for i:= 0; i < 4; i++ {
// Reading the contents in buffer
n, err:= pipeReader.Read(buffer)
// If error is not nil panic
if err != nil {
panic(err)
}
// Prints the content read in buffer
fmt.Printf("%s\n", buffer[:n])
}
}
輸出:
GfG GeeksforGeeks panic:EOF goroutine 1 [running]: main.main() /tmp/sandbox342418232/prog.go:42 +0x2d1
此處,兩個寫入操作的內容是在Close()方法之前寫入的,但它們將返回,但不會返回在Close方法之後寫入的內容,而是會引發EOF錯誤。
範例2:
// Golang program to illustrate the usage of
// io.PipeWriter.Close() function
// Including main package
package main
// Importing fmt and io
import (
"fmt"
"io"
)
// Calling main
func main() {
// Calling Pipe method
pipeReader, pipeWriter:= io.Pipe()
// Calling Close method in go function
go func() {
pipeWriter.Close()
// Calling Write method
pipeWriter.Write([]byte("GfG is a CS-Portal."))
}()
// Creating buffer using make keyword
// of specified length
buffer:= make([]byte, 50)
// Using for loop
for i:= 0; i < 4; i++ {
// Reading the contents in buffer
n, err:= pipeReader.Read(buffer)
// If error is not nil panic
if err != nil {
panic(err)
}
// Prints the content read in buffer
fmt.Printf("%s\n", buffer[:n])
}
}
輸出:
panic:EOF goroutine 1 [running]: main.main() /tmp/sandbox173042396/prog.go:40 +0x2d1
在這裏,在Close方法之前不會執行任何Write操作,因此不會返回任何內容,並且會引發EOF錯誤。
相關用法
- Golang math.Lgamma()用法及代碼示例
- Golang math.Float64bits()用法及代碼示例
- Golang atomic.AddInt64()用法及代碼示例
- Golang atomic.StoreInt64()用法及代碼示例
- Golang reflect.FieldByIndex()用法及代碼示例
- Golang string.Contains用法及代碼示例
- Golang bits.Sub()用法及代碼示例
- Golang io.PipeWriter.CloseWithError()用法及代碼示例
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 io.PipeWriter.Close() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。