type Writer interface { Write(p []byte) (n int, err error) }
file, err := os.Create("test.txt") if err != nil { log.Fatal(err) } defer file.Close() n, err := io.WriteString(file, "Hello, world!") if err != nil { log.Fatal(err) } fmt.Printf("Wrote %d bytes\n", n)
file, err := os.Create("test.txt") if err != nil { log.Fatal(err) } defer file.Close() section := io.NewSectionWriter(file, 10, 5) n, err := section.Write([]byte("world")) if err != nil { log.Fatal(err) } fmt.Printf("Wrote %d bytes\n", n)
file1, err := os.Create("test1.txt") if err != nil { log.Fatal(err) } defer file1.Close() file2, err := os.Create("test2.txt") if err != nil { log.Fatal(err) } defer file2.Close() multiwriter := io.MultiWriter(file1, file2) n, err := multiwriter.Write([]byte("Hello, world!")) if err != nil { log.Fatal(err) } fmt.Printf("Wrote %d bytes\n", n)This code creates two new files called "test1.txt" and "test2.txt", and uses io.MultiWriter to create a new writer that writes to both files. It then writes the byte slice "Hello, world!" to the writer. All of these functions are part of the io package.