io.MultiWriter() Function in Golang with Examples

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.MultiWriter() function
  
// Including main package
package main
  
// Importing fmt, io, bytes, and strings
import (
    "bytes"
    "fmt"
    "io"
    "strings"
)
  
// Calling main
func 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())
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.MultiWriter() function
  
// Including main package
package main
  
// Importing fmt, io, bytes, and strings
import (
    "bytes"
    "fmt"
    "io"
    "strings"
)
  
// Calling main
func 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())
}

chevron_right


Output:

Number of bytes in the buffer: 29
Buffer1: GeeksforGeeks
is
a
CS-Portal!
Buffer2: GeeksforGeeks
is
a
CS-Portal!



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.