Package os provides a platform-independent interface to operating system functionality.
The design is Unix-like, although the error handling is Go-like; failing calls return values of type error rather than error numbers.
// read file file, err := os.Open("file.go") if err != nil { log.Fatal(err) } // read file content imageBytes, err := os.ReadFile(imagePath) if err != nil { log.Fatal(err) } // read file byte count data := make([]byte, 100) count, err := file.Read(data) if err != nil { log.Fatal(err) } fmt.Printf("read %d bytes: %q\n", count, data[:count])
// write to byte array byteArray := []byte("to be written to a file\n") err := os.WriteFile("file.txt", byteArray, permissions) if err != nil { ... }