package main import ( "bytes" "fmt" ) func main() { var buffer bytes.Buffer buffer.Write([]byte("Hello ")) buffer.Write([]byte("World")) fmt.Println(buffer.String()) }
Hello World
package main import ( "bytes" "fmt" ) func main() { var buffer bytes.Buffer buffer.WriteString("Hello World") data := buffer.Bytes() fmt.Println(string(data)) }
Hello World
package main import ( "bytes" "io" "os" ) func main() { var buffer bytes.Buffer buffer.Write([]byte("Hello World")) file, _ := os.Create("output.txt") defer file.Close() buffer.WriteTo(file) }This code writes the string "Hello World" to a file called "output.txt".