io.CopyBuffer() 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
CopyBuffer() function in Go language is the same as
Copy() method but the only exception is that it exhibits through the supplied buffer if one is needed instead of allotting a provisional one. Where in case src is implemented by WriterTo or dst is implemented by ReaderFrom then no buffer will be used to perform the copy operation. 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 CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)
Here, "dst" is the destination, "src" is the source from where the content is copied to the destination, and "buf" is the buffer that allows permanent space in memory.
Return value: It returns the total number of bytes of type int64 that are copied to the "dst" and also returns the first error that is faced while copying from src to dst, if any. However, if the buffer is nil then one is allotted else if the length of the buffer is zero then CopyBuffer panics.
Below examples illustrates the use of the above method:
Example 1:
C
// Golang program to illustrate the usage of
// io.CopyBuffer() function
// Including main package
package main
// Importing fmt, io, os, and strings
import (
"fmt"
"io"
"os"
"strings"
)
// Calling main
func main() {
// Defining source
src := strings.NewReader("GfG\n")
// Defining destination using Stdout
dst := os.Stdout
// Defining buffer of length one using
// make keyword
buffer := make([]byte, 1)
// Calling CopyBuffer method with its parameters
bytes, err := io.CopyBuffer(dst, src, buffer)
// If error is not nil then panics
if err != nil {
panic(err)
}
// Prints output
fmt.Printf("The number of bytes are: %d\n", bytes)
}
Output:
GfG
The number of bytes are: 4
Example 2:
C
// Golang program to illustrate the usage of
// io.CopyBuffer() function
// Including main package
package main
// Importing fmt, io, os, and strings
import (
"fmt"
"io"
"os"
"strings"
)
// Calling main
func main() {
// Defining two sources
src1 := strings.NewReader("GfG\n")
src2 := strings.NewReader("GeeksforGeeks is a CS-Portal\n")
// Defining destination using Stdout
dst := os.Stdout
// Defining buffer of length one using
// make keyword
buffer := make([]byte, 1)
// Calling CopyBuffer method with its parameters
bytes1, err := io.CopyBuffer(dst, src1, buffer)
bytes2, err := io.CopyBuffer(dst, src2, buffer)
// If error is not nil then panics
if err != nil {
panic(err)
}
// Prints output
fmt.Printf("The number of bytes are: %d\n", bytes1)
fmt.Printf("The number of bytes are: %d\n", bytes2)
}
Output:
GfG
GeeksforGeeks is a CS-Portal
The number of bytes are: 4
The number of bytes are: 29
Here, in the above example NewReader() method of strings is used from where the content to be copied is read. And "Stdout" is used here in order to create a default file descriptor where the copied content is written. Moreover, the same buffer is reused above while calling CopyBuffer() method, and no extra buffer is required to allocate.
Similar Reads
io.Copy() 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 Copy() function in Go language is used to copy from the stated src i.e, source to the dst i.e, destination till either the
3 min read
io.CopyN() 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 CopyN() function in Go language is used to copy "n" bytes from source to the destination. And if dst is implemented by the
3 min read
reflect.Copy() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Copy() Function in Golang is used to copies the contents of the source into destination until either destination
2 min read
time.AfterFunc() Function in Golang With Examples In Go language, time packages supplies functionality for determining as well as viewing time. The AfterFunc() function in Go language is used to wait for the duration of time to pass and after that, it calls the defined function "f" in its own go-routine. Moreover, this function is defined under the
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
errors.New() Function in Golang with Examples Errors package in Golang is used to implement the functions to manipulate the errors. errors.New()function returns an error that formats like given text. Each call to New returns distinct error value indeed in the event that the content is indistinguishable. Syntax: func New(text string) error It re
1 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.PipeReader.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 PipeReader.Close() function in Go language is used to close the reader. However, successive writes to the PipeWriter i.e, w
3 min read
reflect.Cap() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Cap() Function in Golang is used to get the v's capacity. To access this function, one needs to imports the refle
1 min read
reflect.ChanOf() Function in Golang with Examples Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.ChanOf() Function in Golang is used to get the channel type with the given direction and element type, i.e., t re
1 min read