User Tools

Site Tools


go:stdlib:os_open

os.Open

What is it?

`os.Open(name)` opens the named file for reading and returns an `*os.File`.

What is it used for?

  • Read file contents.
  • Work with file handles for streaming reads.

Example

f, err := os.Open("data.txt")
if err != nil {
    return err
}
defer f.Close()
 
b, err := io.ReadAll(f)
if err != nil {
    return err
}
_ = b

Notes

  • Always `defer f.Close()` to release resources.
  • For read+write or create, use `os.OpenFile` with flags.
  • For small files, `os.ReadFile` is a convenient helper.

Hard words (English)

  • handle /ˈhændl/: tay cầm file (descriptor)
  • streaming /ˈstriːmɪŋ/: đọc theo luồng (từng phần)
  • release /rɪˈliːs/: giải phóng
  • flag /flæɡ/: cờ tuỳ chọn
go/stdlib/os_open.txt · Last modified: by phong2018