How to read and write structured data (e.g., JSON, XML) using the io package in Go?

To read and write structured data like JSON or XML using the io package in Go, you can make use of additional packages such as encoding/json and encoding/xml. Here's an example of how to read and write JSON and XML data using the io package:

Reading JSON:

package main import ( "encoding/json" "fmt" "io/ioutil" "os" ) type Person struct { Name string `json:"name"` Age int `json:"age"` Email string `json:"email"` } func main() { // Open the JSON file file, err := os.Open("data.json") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() // Read the JSON data from the file data, err := ioutil.ReadAll(file) if err != nil { fmt.Println("Error reading file:", err) return } // Unmarshal the JSON data into a struct var person Person err = json.Unmarshal(data, &person) if err != nil { fmt.Println("Error unmarshaling JSON:", err) return } // Access the data fmt.Println("Name:", person.Name) fmt.Println("Age:", person.Age) fmt.Println("Email:", person.Email) }

Writing JSON:

package main import ( "encoding/json" "fmt" "io/ioutil" "os" ) type Person struct { Name string `json:"name"` Age int `json:"age"` Email string `json:"email"` } func main() { // Create a new Person with data person := Person{ Name: "John Doe", Age: 30, Email: "[email protected]", } // Marshal the Person struct into JSON bytes data, err := json.Marshal(person) if err != nil { fmt.Println("Error marshaling JSON:", err) return } // Write the JSON bytes to a file err = ioutil.WriteFile("data.json", data, 0644) if err != nil { fmt.Println("Error writing to file:", err) return } fmt.Println("Data written to data.json") }

Reading XML:

package main import ( "encoding/xml" "fmt" "io/ioutil" "os" ) type Person struct { Name string `xml:"name"` Age int `xml:"age"` Email string `xml:"email"` } func main() { // Open the XML file file, err := os.Open("data.xml") if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() // Read the XML data from the file data, err := ioutil.ReadAll(file) if err != nil { fmt.Println("Error reading file:", err) return } // Unmarshal the XML data into a struct var person Person err = xml.Unmarshal(data, &person) if err != nil { fmt.Println("Error unmarshaling XML:", err) return } // Access the data fmt.Println("Name:", person.Name) fmt.Println("Age:", person.Age) fmt.Println("Email:", person.Email) }

Writing XML:

package main import ( "encoding/xml" "fmt" "io/ioutil" ) type Person struct { Name string `xml:"name"` Age int `xml:"age"` Email string `xml:"email"` } func main() { // Create a new Person with data person := Person{ Name: "John Doe", Age: 30, Email: "[email protected]", } // Marshal the Person struct into XML bytes data, err := xml.Marshal(person) if err != nil { fmt.Println("Error marshaling XML:", err) return } // Write the XML bytes to a file err = ioutil.WriteFile("data.xml", data, 0644) if err != nil { fmt.Println("Error writing to file:", err) return } fmt.Println("Data written to data.xml") }

Ensure that you have the required JSON/XML files (data.json and data.xml) in the same directory as your Go code.