Design a public API for a file copying library in Go — function headers and types, as developer-friendly as possible.
sobes.tech AI
Answer from AI
go package filecopy
import "io"
// CopyFile copies a file from src to dst. // If dst does not exist, a new file is created. // Returns the number of bytes copied and an error if it occurred. func CopyFile(srcPath, dstPath string) (int64, error)
// CopyFileWithOptions copies a file with additional options. // For example, you can specify a buffer, whether to overwrite the file, copy permissions, etc. type CopyOptions struct { Overwrite bool // whether to overwrite the file if it exists BufferSize int // buffer size for copying, 0 — use default value PreservePermissions bool // copy permissions }
func CopyFileWithOptions(srcPath, dstPath string, opts CopyOptions) (int64, error)
// Copy copies data from io.Reader to io.Writer with options. // Allows copying not only files but also any data streams. func Copy(dst io.Writer, src io.Reader, opts CopyOptions) (int64, error)
Such an interface is convenient because:
- There is a simple CopyFile method for basic file copying.
- There is an extended method with options for more fine-tuned settings.
- There is a universal Copy for streams, increasing flexibility.
- Clear types and names are used, consistent with Go style.