當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang io.PipeReader.Read()用法及代碼示例

在Go語言中,io軟件包為I /O原語提供基本接口。它的主要工作是封裝此類原始之王的正在進行的實現。 Go語言中的PipeReader.Read()函數用於實現Read的標準接口。它從管道中讀取信息並將其阻塞,直到出現寫入器或管道的寫入端關閉為止。而且,此函數在io包下定義。在這裏,您需要導入“io”包才能使用這些函數。

用法:

func (r *PipeReader) Read(data []byte) (n int, err error)

此處,“r”是指向PipeReader的指針。其中,PipeReader是讀取管道的一半,而“data”是指定長度的字節片,並將寫入的數據讀取到其中。

返回值:它返回讀取的字節數和一個錯誤(如果有)。但是,如果管道的寫端因錯誤而關閉,則該錯誤將作為err返回,否則返回的err為EOF錯誤。

範例1:



// Golang program to illustrate the usage of 
// io.PipeReader.Read() function 
  
// Including main package 
package main 
  
// Importing fmt and io 
import ( 
    "fmt"
    "io"
) 
  
// Calling main 
func main() { 
  
    // Calling Pipe method 
    pipeReader, pipeWriter:= io.Pipe() 
  
    // Writing data to the pipe 
    go func() { 
        pipeWriter.Write([]byte("GfG")) 
        pipeWriter.Write([]byte("GeeksforGeeks")) 
        pipeWriter.Write([]byte("GfG is a CS-Portal.")) 
  
        // Closing write half of the pipe 
        pipeWriter.Close() 
  
        // Again calling Write method 
        pipeWriter.Write([]byte("Author!")) 
    }() 
  
    // Defining data parameter of Read method 
    data:= make([]byte, 20) 
  
    // Using for loop 
    for i:= 0; i < 3; i++ { 
  
        // Calling pipeReader.Read() method 
        n, err:= pipeReader.Read(data) 
  
        // If error is not nil panic 
        if err != nil { 
            panic(err) 
        } 
  
        // Prints the content read in buffer 
        fmt.Printf("%s\n", data[:n]) 
  
        // Prints number of bytes read 
        fmt.Printf("%v\n", n) 
    } 
}

輸出:

GfG
3
GeeksforGeeks
13
GfG is a CS-Portal.
19

在此,由於在“for”循環運行之前管道的寫端沒有關閉,因此不會返回錯誤。

範例2:

// Golang program to illustrate the usage of 
// io.PipeReader.Read() function 
  
// Including main package 
package main 
  
// Importing fmt and io 
import ( 
    "fmt"
    "io"
) 
  
// Calling main 
func main() { 
  
    // Calling Pipe method 
    pipeReader, pipeWriter:= io.Pipe() 
  
    // Writing data to the pipe 
    go func() { 
        pipeWriter.Write([]byte("GfG")) 
        pipeWriter.Write([]byte("GeeksforGeeks")) 
        pipeWriter.Write([]byte("GfG is a CS-Portal.")) 
  
        // Closing write half of the pipe 
        pipeWriter.Close() 
  
        // Again calling Write method 
        pipeWriter.Write([]byte("Author!")) 
    }() 
  
    // Defining data parameter of Read method 
    data:= make([]byte, 20) 
  
    // Using for loop 
    for i:= 0; i < 4; i++ { 
  
        // Calling pipeReader.Read() method 
        n, err:= pipeReader.Read(data) 
  
        // If error is not nil panic 
        if err != nil { 
            panic(err) 
        } 
  
        // Prints the content read in buffer 
        fmt.Printf("%s\n", data[:n]) 
  
        // Prints number of  bytes read 
        fmt.Printf("%v\n", n) 
    } 
}

輸出:

GfG
3
GeeksforGeeks
13
GfG is a CS-Portal.
19
panic:EOF

goroutine 1 [running]:
main.main()
    /tmp/sandbox634835876/prog.go:43 +0x364

在這裏,在for循環的第三次迭代之後,由於管道的寫端被關閉,因此返回了EOF錯誤。




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 io.PipeReader.Read() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。