Skip to content
Related Articles

Related Articles

Improve Article

io.MultiWriter() Function in Golang with Examples

  • Last Updated : 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 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())
}

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 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())
}

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
Recommended Articles
Page :