io.MultiWriter() Function in Golang with Examples Last Updated : 05 May, 2020 Comments Improve Suggest changes Like Article Like Report 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: C // 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: C // 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! Comment More infoAdvertise with us Next Article io.MultiWriter() Function in Golang with Examples nidhi1352singh Follow Improve Article Tags : Go Language Golang-io Similar Reads io.MultiReader() 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 MultiReader() function in Go language is used to return a "Reader" that is the logical concatenation of all the stated inpu 3 min read io.LimitReader() 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 LimitReader() function in Go language is used to return a "Reader" that reads from the stated "r" but it pauses if the EOF 2 min read io.PipeWriter.Write() 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 PipeWriter.Write() function in Go language is used to implement the standard interface of the Write. It writes information 3 min read io.Pipe() 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 Pipe() function in Go language is used to create a concurrent in-memory pipe and can be applied in order to link the code t 3 min read io.TeeReader() 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 TeeReader() function in Go language is used to return a "Reader" that reads from the stated "r" and then writes it to the s 3 min read io.PipeWriter.Close() 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 PipeWriter.Close() function in Go language is used to close the writer. However, successive reads from the PipeReader i.e, 3 min read io.ReadFull() 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 ReadFull() function in Go language is used to read from the stated reader "r" into the stated buffer "buf" and the bytes co 3 min read io.WriteString() 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 WriteString() function in Go language is used to write the contents of the stated string "s" to the writer "w", which takes 2 min read io.NewSectionReader() Function in Golang with Examples In Go language, io packages supplies fundamental interfaces to the I/O primitives. And its principle job is to enclose the ongoing implementations of such king of primitives. The NewSectionReader() function in Go language is used to return a SectionReader that reads from the stated reader "r" which 3 min read io.ReadAtLeast() 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 ReadAtLeast() function in Go language is used to read from the stated reader "r" into the stated buffer "buf" until it has 3 min read Like