Copy file
Tuesday 11 October 2022

#go doesn’t have a function to copy files. the following is a simple implementation.

It seams it uses one Copy function so I guess this is suitable for small files as large files will take too long and there is no way to know the progress or cancel it.

So use it for files that you expect to be small.

 1func copyFile(src, dst string) error {
 2	in, err := os.Open(src)
 3	if err != nil {
 4		return err
 5	}
 6	defer in.Close()
 7
 8	out, err := os.Create(dst)
 9	if err != nil {
10		return err
11	}
12	defer out.Close()
13
14	_, err = io.Copy(out, in)
15	if err != nil {
16		return err
17	}
18	return out.Close()
19}

See Also