$ go doc os package os // import "os"
Package os provides a platform-independent interface to operating system functionality. The design is Unix-like, although the error handling is Go-like; failing calls return values of type error rather than error numbers. Often, more information is available within the error. For example, if a call that takes a file name fails, such as Open or Stat, the error will include the failing file name when printed and will be of type *PathError, which may be unpacked for more information.
The os interface is intended to be uniform across all operating systems. Features not generally available appear in the system-specific package syscall.
Here is a simple example, opening a file and reading some of it.
file, err := os.Open("file.go") // For read access. if err != nil { log.Fatal(err) }
If the open fails, the error string will be self-explanatory, like
open file.go: no such file or directory
The file's data can then be read into a slice of bytes. Read and Write take their byte counts from the length of the argument slice.
data := make([]byte, 100) count, err := file.Read(data) if err != nil { log.Fatal(err) } fmt.Printf("read %d bytes: %q\n", count, data[:count])
Note: The maximum number of concurrent operations on a File may be limited by the OS or the system. The number should be high, but exceeding it may degrade performance or cause other issues.
const O_RDONLY int = syscall.O_RDONLY ... const SEEK_SET int = 0 ... const PathSeparator = '/' ... const ModeDir = fs.ModeDir ... const DevNull = "/dev/null" var ErrInvalid = fs.ErrInvalid ... var Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin") ... var Args []string var ErrProcessDone = errors.New("os: process already finished") func Chdir(dir string) error func Chmod(name string, mode FileMode) error func Chown(name string, uid, gid int) error func Chtimes(name string, atime time.Time, mtime time.Time) error func Clearenv() func DirFS(dir string) fs.FS func Environ() []string func Executable() (string, error) func Exit(code int) func Expand(s string, mapping func(string) string) string func ExpandEnv(s string) string func Getegid() int func Getenv(key string) string func Geteuid() int func Getgid() int func Getgroups() ([]int, error) func Getpagesize() int func Getpid() int func Getppid() int func Getuid() int func Getwd() (dir string, err error) func Hostname() (name string, err error) func IsExist(err error) bool func IsNotExist(err error) bool func IsPathSeparator(c uint8) bool func IsPermission(err error) bool func IsTimeout(err error) bool func Lchown(name string, uid, gid int) error func Link(oldname, newname string) error func LookupEnv(key string) (string, bool) func Mkdir(name string, perm FileMode) error func MkdirAll(path string, perm FileMode) error func MkdirTemp(dir, pattern string) (string, error) func NewSyscallError(syscall string, err error) error func Pipe() (r *File, w *File, err error) func ReadFile(name string) ([]byte, error) func Readlink(name string) (string, error) func Remove(name string) error func RemoveAll(path string) error func Rename(oldpath, newpath string) error func SameFile(fi1, fi2 FileInfo) bool func Setenv(key, value string) error func Symlink(oldname, newname string) error func TempDir() string func Truncate(name string, size int64) error func Unsetenv(key string) error func UserCacheDir() (string, error) func UserConfigDir() (string, error) func UserHomeDir() (string, error) func WriteFile(name string, data []byte, perm FileMode) error type DirEntry = fs.DirEntry func ReadDir(name string) ([]DirEntry, error) type File struct{ ... } func Create(name string) (*File, error) func CreateTemp(dir, pattern string) (*File, error) func NewFile(fd uintptr, name string) *File func Open(name string) (*File, error) func OpenFile(name string, flag int, perm FileMode) (*File, error) type FileInfo = fs.FileInfo func Lstat(name string) (FileInfo, error) func Stat(name string) (FileInfo, error) type FileMode = fs.FileMode type LinkError struct{ ... } type PathError = fs.PathError type ProcAttr struct{ ... } type Process struct{ ... } func FindProcess(pid int) (*Process, error) func StartProcess(name string, argv []string, attr *ProcAttr) (*Process, error) type ProcessState struct{ ... } type Signal interface{ ... } var Interrupt Signal = syscall.SIGINT ... type SyscallError struct{ ... } |