io.MultiWriter() Function in Golang with Examples - GeeksforGeeks

05 May, 2020
In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The MultiWriter() function in Go language is used to create a writer that copies its writes to each and every given writers, which is the same as the Unix command tee(1). Here, each and every write is written to each of the included writer, one by one. Moreover, this function is defined under the io package. Here, you need to import the “io” package in order to use these functions.
Syntax:
func MultiWriter(writers ...Writer) Writer

Here, “writers” is the number of writers stated as a parameter in this function.
Return value: It returns a Writer which includes the number of bytes present in the stated buffer and also returns an error if any. And if a stated writer returns an error then the entire write operation ceases and doesn’t extend down the list.
Example 1:
// Golang program to illustrate the usage of// io.MultiWriter() function  // Including main packagepackage main  // Importing fmt, io, bytes, and stringsimport (    "bytes"    "fmt"    "io"    "strings")  // Calling mainfunc main() {      // Defining reader using NewReader method    reader := strings.NewReader("Geeks")      // Defining two buffers    var buffer1, buffer2 bytes.Buffer      // Calling MultiWriter method with its parameters    writer := io.MultiWriter(&buffer1, &buffer2)      // Calling Copy method with its parameters    n, err := io.Copy(writer, reader)      // If error is not nil then panics    if err != nil {        panic(err)    }      // Prints output    fmt.Printf("Number of bytes in the buffer: %v\n", n)    fmt.Printf("Buffer1: %v\n", buffer1.String())    fmt.Printf("Buffer2: %v\n", buffer2.String())}
Output:
Number of bytes in the buffer: 5
Buffer1: Geeks
Buffer2: Geeks

Here, Copy() method is used to return the number of bytes contained in the buffer. And here the content of both the buffer is the same as the stated writer duplicates its write to all the other writers.
Example 2:
// Golang program to illustrate the usage of// io.MultiWriter() function  // Including main packagepackage main  // Importing fmt, io, bytes, and stringsimport (    "bytes"    "fmt"    "io"    "strings")  // Calling mainfunc main() {      // Defining reader using NewReader method    reader := strings.NewReader("GeeksforGeeks\nis\na\nCS-Portal!")      // Defining two buffers    var buffer1, buffer2 bytes.Buffer      // Calling MultiWriter method with its parameters    writer := io.MultiWriter(&buffer1, &buffer2)      // Calling Copy method with its parameters    n, err := io.Copy(writer, reader)      // If error is not nil then panics    if err != nil {        panic(err)    }      // Prints output    fmt.Printf("Number of bytes in the buffer: %v\n", n)    fmt.Printf("Buffer1: %v\n", buffer1.String())    fmt.Printf("Buffer2: %v\n", buffer2.String())}
Output:
Number of bytes in the buffer: 29
Buffer1: GeeksforGeeks
is
a
CS-Portal!
Buffer2: GeeksforGeeks
is
a
CS-Portal!

Golang-io
Go Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
strings.Replace() Function in Golang With Examples
How to Split a String in Golang?
Arrays in Go
Golang Maps
Slices in Golang
How to convert a string in lower case in Golang?
How to compare times in Golang?
How to Trim a String in Golang?
Different Ways to Find the Type of Variable in Golang
How to Parse JSON in Golang?