os.File represents an open file in Go and provides operations for reading, writing, changing permissions, creating files, and other file-system tasks. Opening a path returns a *File and an error. section 3.1.2
Choose the opening function according to the intended access:
- Use os.Open for read-only access. section 3.1.2
- Use os.Create for read/write access when replacing an existing file is acceptable; it truncates existing contents. section 3.1.2
- Use os.OpenFile when configurable read/write behavior is required. section 3.1.2
Open and close a file
f, err := os.Open(filePath) if err != nil { return nil, err } defer f.Close()
