好好爱自己!

golang io.Copy()的使用

原文:https://www.geeksforgeeks.org/io-copy-function-in-golang-with-examples/

-----------------------------------------

io.Copy() Function in Golang with Examples

Last Updated: 05-05-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 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 EOF i.e, end of file is attained on src or an error is thrown. Here, when src is implemented by WriterTo interface then the copy is implemented by a call to src.WriteTo(dst). else, if dst is implemented by the ReaderFrom interface then the copy is implemented by a call to dst.ReadFrom(src). 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 Copy(dst Writer, src Reader) (written int64, err error)

Here, “dst” is the destination and “src” is the source from where the content is copied to the destination.
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. And if there is no error in copying then “nil” is returned.

Below examples illustrates the use of above method:

Example 1:



filter_none

 

 

brightness_4

// Golang program to illustrate the usage of
// io.Copy() 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 Copy method with its parameters
    bytes, err := io.Copy(dst, src)
  
    // 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:

GeeksforGeeks
The number of bytes are: 14

Example 2:

filter_none

 

 

brightness_4

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

Nidhi: F
Rahul: M
Nisha: F
The number of bytes are: 27

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.

posted @   立志做一个好的程序员  阅读(1864)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2019-08-22 How to parse unix timestamp to time.Time
2019-08-22 redis 安装报错 jemalloc/jemalloc.h: No such file or directory。
2018-08-22 ubuntu创建sudo 用户
2018-08-22 vsftpd 配置:chroot_local_user与chroot_list_enable详解
2018-08-22 php 中 拓展 xdebug的完全理解
2018-08-22 subline 配置,本地项目代码下断点来调试远程项目
2018-08-22 php中自定义事件---事件驱动

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示