os.Open opens a named file for reading. It returns a pointer to an os.File descriptor and an error, which callers must check. section 3.1.2
Open and close a file safely
f, err := os.Open(filePath) if err != nil { return nil, err } defer f.Close() section 3.1.2
Choose the file-opening function according to the intended operation:
- Use os.Open for read-only access. section 3.1.2
- Use os.Create to create or truncate a file for reading and writing. section 3.1.2
- Use os.OpenFile when flags are needed, such as appending without discarding existing contents. section 3.1.2
Listing 3.3 bookworm.go: Opening a file
f, err := os.Open(filePath) #1
if err != nil { #2
return nil, err
}
defer f.Close() #3
View the complete opening pattern, including error propagation and deferred cleanup. section 3.1.2

