How do I read and write files safely in Go?

Go, file operations, reading and writing files, error handling
This example demonstrates how to read and write files safely in Go, focusing on error handling and security.
package main import ( "fmt" "io/ioutil" "log" "os" ) func main() { // Writing to a file safely data := []byte("Hello, World!") err := ioutil.WriteFile("example.txt", data, 0644) if err != nil { log.Fatalf("Failed to write to file: %v", err) } fmt.Println("Data written to file successfully!") // Reading from a file safely content, err := ioutil.ReadFile("example.txt") if err != nil { log.Fatalf("Failed to read from file: %v", err) } fmt.Println("File content:", string(content)) }

Go file operations reading and writing files error handling