var buf bytes.Buffer buf.WriteString("hello ") buf.WriteString("world") fmt.Println(buf.String()) // Output: "hello world"
file, err := os.Open("input.txt") if err != nil { log.Fatal(err) } defer file.Close() var buf bytes.Buffer _, err = io.Copy(&buf, file) if err != nil { log.Fatal(err) } err = ioutil.WriteFile("output.txt", buf.Bytes(), 0644) if err != nil { log.Fatal(err) }In this example, we first open a file for reading using the os.Open() function. Next, we create a buffer to store the file contents using the bytes.Buffer() constructor. We then use the io.Copy() function to read the contents of the file into the buffer. Finally, we write the contents of the buffer to a new file using the ioutil.WriteFile() function.