Go
File Operations
Read and write file operations.
By EZ4Code Team
fileio
Code
package main
import (
"fmt"
"os"
)
func main() {
// Write file
err := os.WriteFile("test.txt", []byte("Hello Go"), 0644)
if err != nil {
panic(err)
}
// Read file
data, err := os.ReadFile("test.txt")
if err != nil {
panic(err)
}
fmt.Println(string(data))
// Append write
f, _ := os.OpenFile("test.txt", os.O_APPEND|os.O_WRONLY, 0644)
defer f.Close()
f.WriteString("\nAppended line")
}Explanation
The os package provides file read/write; OpenFile supports multiple open modes.