io.CopyBuffer() 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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

GfG
The number of bytes are: 4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.




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.