package main import ( "bytes" "fmt" ) func main() { var buffer bytes.Buffer buffer.WriteString("Hello ") buffer.WriteString("World!") fmt.Println(buffer.String()) }
Hello World!
package main import ( "bytes" "encoding/json" "fmt" ) type Person struct { Name string Age int } func main() { person := Person{Name: "John", Age: 30} var buffer bytes.Buffer err := json.NewEncoder(&buffer).Encode(person) if err != nil { panic(err) } fmt.Println(buffer.String()) }
{"Name":"John","Age":30}This code creates a new `Person` struct, encodes it to JSON using the `json` package, and writes the resulting JSON string to a `bytes.Buffer` using `WriteString`. The contents of the buffer are then printed to the console.