在Go語言中,io軟件包為I /O原語提供基本接口。它的主要工作是封裝此類原始之王的正在進行的實現。 Go語言中的MultiReader()函數用於返回“Reader”,它是所有所述輸入閱讀器的邏輯串聯。但是,這些陳述的閱讀器是按順序閱讀的。在所有指定的輸入返回EOF後,即文件末尾,“Read”將返回EOF。而且,此函數在io包下定義。在這裏,您需要導入“io”包才能使用這些函數。
用法:
func MultiReader(readers ...Reader) Reader
在此,“readers”是所述讀取器的數量。
返回值:它返回“Reader”,它是所有給定輸入讀取器的邏輯串聯。並且,如果任何指定的讀取器返回非零錯誤或非EOF錯誤,則“Read”返回該錯誤。
以下示例說明了上述方法的用法:
範例1:
// Golang program to illustrate the usage of
// io.MultiReader() function
// Including main package
package main
// Importing fmt, io, strings, and os
import (
"fmt"
"io"
"os"
"strings"
)
// Calling main
func main() {
// Defining readers using NewReader method
reader1:= strings.NewReader("Geeks\n")
reader2:= strings.NewReader("GfG\n")
reader3:= strings.NewReader("CS\n")
// Calling MultiReader method with its parameters
r:= io.MultiReader(reader1, reader2, reader3)
// Calling Copy method with its parameters
Reader, err:= io.Copy(os.Stdout, r)
// If error is not nil then panics
if err != nil {
panic(err)
}
// Prints output
fmt.Printf("n:%v\n", Reader)
}
輸出:
Geeks GfG CS n:13
在上麵的示例中,使用Copy()方法以返回所有串聯的讀取器的結果,並使用NewReader()字符串方法從中寫入要讀取的內容。
範例2:
// Golang program to illustrate the usage of
// io.MultiReader() function
// Including main package
package main
// Importing fmt, io, strings, and os
import (
"fmt"
"io"
"os"
"strings"
)
// Calling main
func main() {
// Defining readers using NewReader method
reader1:= strings.NewReader("104\n")
reader2:= strings.NewReader("33.3\n")
reader3:= strings.NewReader("703243242\n")
// Calling MultiReader method with its parameters
r:= io.MultiReader(reader1, reader2, reader3)
// Calling Copy method with its parameters
Reader, err:= io.Copy(os.Stdout, r)
// If error is not nil then panics
if err != nil {
panic(err)
}
// Prints output
fmt.Printf("n:%v\n", Reader)
}
輸出:
104 33.3 703243242 n:19
相關用法
- 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.MultiReader() Function in Golang with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。