Use encoding/json.Marshal to convert a Go struct to a JSON byte slice, then handle any errors that occur. This function automatically serializes exported struct fields into JSON format.
package main
import (
"encoding/json"
"fmt"
"log"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
p := Person{Name: "Alice", Age: 30}
data, err := json.Marshal(p)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(data))
}