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 ReaderFrom interface then the copy is implemented making use of it. 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 CopyN(dst Writer, src Reader, n int64) (written int64, err error)

Here, “dst” is the destination, “src” is the source from where the content is copied to the destination, and “n” is the limit of the number of bytes that is to be copied from the source.

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. Moreover, the number of bytes returned is “n” if and only if the error is “nil”.

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.CopyN() 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("GeeksforGeeks\n")
  
    // Defining destination using Stdout
    dst := os.Stdout
  
    // Calling CopyN method with its parameters
    bytes, err := io.CopyN(dst, src, 5)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("\nThe number of bytes are: %d\n", bytes)
}

chevron_right


Output:

Geeks
The number of bytes are: 5

Here, the number of bytes returned is equal to “n” as error is “nil”.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.CopyN() 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("CS-Portal\n")
  
    // Defining destination using Stdout
    dst := os.Stdout
  
    // Calling CopyN method with its parameters
    bytes, err := io.CopyN(dst, src, 20)
  
    // 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:

CS-Portal
panic: EOF

goroutine 1 [running]:
main.main()
    /tmp/sandbox691649995/prog.go:29 +0x137

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, here the number of bytes returned is less than “n” so an EOF error is thrown above.




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.