How to read and write files

Read files with os.ReadFile and write files with os.WriteFile in Go.

Use os.ReadFile to read a file into a byte slice and os.WriteFile to write a byte slice to a file.

package main

import (
	"fmt"
	"os"
)

func main() {
	// Read file
	data, err := os.ReadFile("input.txt")
	if err != nil {
		fmt.Println("Error reading file:", err)
		return
	}

	// Write file
	err = os.WriteFile("output.txt", data, 0644)
	if err != nil {
		fmt.Println("Error writing file:", err)
		return
	}
}