os.OpenFile opens or creates a file with caller-selected access flags and permissions. It is more general than os.Open and os.Create, but most read-only or ordinary creation tasks can use those simpler functions. section 3.1.2
Use os.OpenFile when the file operation requires control over:
- Appending without discarding existing contents. section 3.1.2
- Creating a file with permissions different from os.Create’s default. section 3.1.2
- Combining access flags such as os.O_APPEND, os.O_CREATE, and os.O_WRONLY. section 3.1.2
Append to a file
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) if err != nil { return err } defer f.Close() // Write data to f without erasing existing contents. section 3.1.2
