Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"io"
"os"
)
const (
fromFileName = "values.json"
toFileName = "values_copy.json"
)
func getFileReader() io.Reader {
fromFileReader, err := os.OpenFile(fromFileName, os.O_RDONLY, 0666)
defer fromFileReader.Close()
if err != nil {
fmt.Println("There is error in reading file contents!", err)
}
return fromFileReader
}
func writeToFile(reader io.Reader, writer io.Writer) (int64, error){
return io.Copy(writer, reader)
}
func main() {
toFileWriter, err := os.OpenFile(toFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
fmt.Println("There is error in file creation!", err)
}
defer toFileWriter.Close()
fromFileReader := getFileReader()
_, err = writeToFile(fromFileReader, toFileWriter)
if err != nil {
fmt.Println("There is error in writing file contents! ", err)
}
// err = toFileWriter.Sync()
// if err != nil {
// fmt.Println("There is error flushing in memory contents ", err)
// }
stat, err := toFileWriter.Stat()
println(stat.Size())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment