Go/Packages

[Go] Package os

llHoYall 2020. 12. 2. 19:32

Package os provides a platform-independent interface to operating system functionality.

The os interface is intended to be uniform across all operating systems.

Features not generally available appear in the system-specific package syscall.

Constants

  • O_RDONLY : read-only
  • O_WRONLY : write-only
  • O_RDWR : read-write
  • O_APPEND : append data when writing
  • O_CREATE : create if none exists
  • O_EXCL : file must not exist
  • O_SYNC : synchronous I/O
  • O_TRUNC : truncate when opened

Variables

Args

Args hold the command-line arguments, starting with the program name.

var Args []string

Methods

Open

Open opens the named file for reading.

If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY.

If there is an error, it will be of type *PathError.

func Open(name string) (*File, error)

Close

Close closes the FIle, rendering it unusable for I/O.

On files that support SetDeadline, any pending I/O operations will be canceled and return immediately with an error.

Close will return an error if it has already been called.

func (f *File) Close() error

Usages

file, err := os.Open("README.md")
if err != nil {
  log.Fatal(err)
}

err = file.Close()
if err != nil {
  log.Fatal(err)
}
반응형