Skip to content
Related Articles

Related Articles

io.CopyBuffer() Function in Golang with Examples
  • Last Updated : 05 May, 2020
GeeksforGeeks - Summer Carnival Banner

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:




// 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:




// 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :